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();
}