gus
gus

Reputation: 365

Updating variable value through a function in php

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>

Upvotes: 1

Views: 5556

Answers (1)

DWright
DWright

Reputation: 9500

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.

Upvotes: 3

Related Questions