1

I am new in web page development, and it seems that this is a basic question but I can't figure out how to solve.

Through a button I need to change the value of a variable in php, specifically every time a button is pushed the value of a variable must increase.

The code is the following:

     <!DOCTYPE html>
     <html lang="en">
     <head>
        <meta charset="utf-8">
        <title>Test</title>
     </head>
     <body>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <script>
             function echoVal()
             {
                 alert("<?php val(); ?>");
             }
        </script>

        <?php
             function val() {
                static $a = 0;
                $a++;
                echo $a;
             } 
        ?>

        <button onclick="echoVal()"> Increase </button>
     </body>
     </html>

The main problem is that the value of the variable $a is always 1, no increasing its value when the button is pushed. I have saved the file with extension .php (I am not sure if that will make any difference, but I just mention it).

Any suggestion to solve this issue?

Thanks in advance for any suggestion.

Edited Code:

     <!DOCTYPE html>
     <html lang="en">
     <head>
        <meta charset="utf-8">
        <title>Test</title>
     </head>
     <body>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <script>
             function echoVal()
             {
                 alert("<?php val(); ?>");
             }
        </script>

        <?php
             function val() {
                static $a = 0;
                $a++;
                $filename ='infoX.txt';
                if (is_writable($filename)) {
                    if (!$handle = fopen($filename, 'w')) {
                        echo "Cannot open file";
                    } else {
                        if (fwrite($handle, $a) == FALSE) {
                            echo "Cannot write to file";
                        } else {
                            echo $a;
                        }
                        fclose($handle);
                    }  
               } else {
                    echo "The file is not writable";
               }
             } 
        ?>

        <button onclick="echoVal()"> Increase </button>
     </body>
     </html>
3
  • You are always initializing the value of $a to 0 within your val() method which seems to be why your variable is always resulting to 1. Commented Jan 12, 2013 at 19:12
  • 5
    Pressing the button doesn't execute any PHP code.... at the point where the user sees the button, php has already finished executing and terminated Commented Jan 12, 2013 at 19:12
  • 2
    To add to what Mark says, view source on your page and it will immediately become clear why the counter does not increase. Commented Jan 12, 2013 at 19:13

1 Answer 1

3

Update: I think this example: https://stackoverflow.com/a/3410025/49251 may show you exactly how do what you are trying to do. My answer below is an attempt to provide context on why your first attempt is not working.


A static in PHP only "lives" for the duration of your script executing.

The script only executes on the server--statics in the script will only be "alive" as long as it takes the server to process the file(s)/script(s) associated with the request. Once the request has been processed and a response has been sent, PHP is gone. Done. Not alive. Finished.

There is no PHP in the browser side and the server is no longer running anything with respect to your request after the response has been sent (some memory caching strategies can be an exception to this, but generally, what I'm describing is the normal way PHP works).

PHP's web execution model is: (1) request, (2) server (often Apache) runs PHP to process code for request, sends response (3) and is completely done.

To do what you are trying to do:

  1. You could either use browser-side code entirely (javascript), or
  2. You could send the result of button clicks to the server, for persistence in a db, and re-request those updated results, by either reloading/regenerating the page or using ajax to get some code on the server to run and updating a portion of the page accordingly.
Sign up to request clarification or add additional context in comments.

6 Comments

BTW, @jon 's suggestion to look at the source in the browser, will be really helpful for understanding all of this in action.
Thanks a lot for the answer, but I am using PHP since I need to save the variable $a into a file, as the updated code shows. I know that the writing to file procedure works, and I could not find any method to do the same using jquery (and I could not understand completely ajax, and I am trying to avoid using sql). Any suggestion for what I am trying to do?.
For a fairly classic PHP approach, embed the button in a form and submit the form when the button is clicked. ON the server side, write the incremented click value into a database, and resend the page with the new value in it.
Again, make sure you read the page source for your current example in you browser. You'll see that the alert looks like alert("1"), right? This shows you that php on the server side is always starting with 0 and incrementing to 1 and then writing 1 to your alert string. You need something (say a form submit) to tell the server that something needs to change.
See the link in the Update at the beginning of my answer. It might help you move forward!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.