0

I want to call PHP page using AJAX but I don't want to reload PHP classes each time, for example, on first time running AJAX, PHP page load and set data, but on second time running AJAX, php get the same data from first time.

function test() {
  $.ajax({
    url: "./test.php",
    method: "post",
    success: function(data) {
      $(document).find("body").append(`<p>${data}</p>`);
    }
  });
}
<button type="button" onclick="test()">Click Me!</button>
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>

//test.php
class Test{
    private $name = null;

    public function setName($name){
        $this->name = $name;
    }

    public function getName(){
        return $this->name;
    }
}

$class = new Test;

if( $class->getName() == null ){
    echo "oops";
    $class->setName("pong");
} else {
    echo $class->getName();
}
3
  • 1
    What's your question? What have you tried? Where are you stuck? This is currently a specification, not a question. Stack Overflow isn't a free coding service. Commented Jun 25, 2021 at 8:03
  • i just want to ask when calling ajax multiple times i want to store data in php class instance and on every call i want data from that instance.
    – Rishabh
    Commented Jun 25, 2021 at 8:10
  • Just adding "I want to ask" before a specification doesn't make it into a question. What have you tried and where are you stuck? What is your specific issue? You need to show us your attempt and explain the issue. The question is currently too vague and unfocused. We're here to help you with specific and concrete issues. Please read how to ask (including all referenced pages) Commented Jun 25, 2021 at 8:13

1 Answer 1

-1
// Fire off the request to /form.php
request = $.ajax({
    url: "/form.php",
    type: "post",
    data: serializedData
});

// Callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
    // Log a message to the console
 
});

// Callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
    // Log the error to the console
    console.error(
        "The following error occurred: "+
        textStatus, errorThrown
    );
});

// if the request failed or succeeded
request.always(function () {
    //add your logic 
});
6
  • Before we should answer the question, the OP needs to show what they've tried and explain where they're stuck. And why are you assuming that they are using jQuery? Commented Jun 25, 2021 at 8:05
  • 1
    saving response of request will not help bcz i want to store response in php class instance and on next time calling ajax i want data from that instance.
    – Rishabh
    Commented Jun 25, 2021 at 8:06
  • @MagnusEriksson people who asked this question, how can we think they can always point where they stuck, they might don't know what will be easier for them, so I just did the trick, so he can speak up and for the beginner jQuery probably easier than ES to understand.
    – Arab
    Commented Jun 25, 2021 at 11:59
  • 1
    @Rishabh you can store data in session (temporary) / database.
    – Arab
    Commented Jun 25, 2021 at 12:03
  • @Arab - This site does actually require people to be able to do some basic debugging themselves before they ask. If they don't even know where they are stuck, then I would say they need to take a step back and start over, implementing one thing, get that to work, then the next thing and so on, doing one thing at the time. There's no real reason why someone wouldn't know where they are stuck. Commented Jun 25, 2021 at 12:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.