I have the following situation:
I have a php class called 'Test.php'. This class has two methods: method1 and method2
<?php
class Test {
private $contador;
public function __construct() {
$this->contador = 0;
}
public function method1(){
echo json_encode($this->contador++);
}
public function method2(){
return $this->contador++;
}
}
My problem takes place when I access the Test class with my index.php file.
<!DOCTYPE html>
<?php
require_once 'Test.php';
$test = new Test();
?>
<html>
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(
function () {
$("#someButtonID").click(
function () {
$("#someInputID").val("<?php $test->method1(); ?>");
}
);
}
);
</script>
</head>
<body>
<input id="someInputID" type="text">
<button id="someButtonID" type="submit" value="Action">
Action
</button>
</body>
</html>
What happens: When I click the button 'Action', the PHP code is successfully executed the first time, BUT when I keep pressing it, the code doesn't update and the input value doesn't chance at all. It happens because when the index.php is loaded, the server side execute the Test.php and generate the previous values and it's all. When I press the button for the second time, the index.php isn't able to request the next information to the object on the server side.
The scenario: The 'Test' class represents a dinamic information and I will need to recover it on demand at my main page.
I've looked up how to request this information in a way that when I press a button, I update the content without reloading the page, so just a specific block would be updated, not all the objects that the page makes use.
So, my question is: How do I update the input value on demand requesting its value from a php class?
Thank you all very much for taking time to answer this questions.
Below are some links I found, but couldn't help me as I needed.
How to access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?>
http://api.jquery.com/jquery.get/