0

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/

Accessing a PHP class function through AJAX

How to manipulate PHP object with Jquery

EternalHour
  • 8,308
  • 6
  • 38
  • 57
  • 1
    Because PHP is only executed on the page load once. All you are doing in JavaScript for PHP isn't executing the function `.click`. But giving in the value of the `method1(); ?>` when the page loaded, afterwards nothing would happen because PHP isn't running anymore. So for running it many times you AJAX, although you will need to store the counter (`$contador`) in a session. – Spencer Wieczorek Jan 18 '15 at 23:46

1 Answers1

0

On the client side, you need to use AJAX. On the server side you'll need a handler that can manage the calls for you so you're not accessing the class directly. Maybe something like this will work for you.

HTML

//put PHP before any HTML on the page//
<?php
session_start();
?>
<button id="someButtonID" type="submit" value="method1">

Javascript

$(function() {
    $("#someButtonID").on("click", function() {

    var $value = $(this).attr("value");
    var $method = "method:"+$value;

        $.ajax({
            type: "POST",
            data: {$method},
            url: "classHandler.php",
            dataType: "html",
            success: function(data) {
            // callback from classHandler.php goes here //
            }
        }); 
    });
});

PHP class

class Test {
    private $contador;

    public function __construct() {
        session_start();
        $_SESSION['contador'] = 0;
        $this->contador = $_SESSION['contador'];
    }

    public function method1(){
        echo json_encode($this->contador++);
    }

    public function method2(){
        return $this->contador++;
    }
}

classHandler.php

session_start();
require_once 'Test.php';

// make sure nobody has tampered with the value //
if (ctype_alpha($_POST['method'])) {
  $test = new Test();
}
// whatever you expect your class to do //
if($_POST['method'] === "method1") {
     $result = $test->method1();
}

echo $result;
EternalHour
  • 8,308
  • 6
  • 38
  • 57