Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9118301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:07:02+00:00 2026-06-17T05:07:02+00:00

I am new in web page development, and it seems that this is a

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:07:03+00:00Added an answer on June 17, 2026 at 5:07 am

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to web development, I'm sure there's a simple answer to this, but
I'm new at web development with .NET, and I'm currently studying a page where
I am new to Web page design and creation - but I need to
I'm reading a web page using HttpClient like this: httpclient = new DefaultHttpClient(); httpget
I've got a web page where the user can add a new item to
new on rails and using windows for now,, i have web page that user
Am very New to Web Page Development. In my website i have the help
I am somewhat new to web development, so I'm not sure how stupid this
I'm very new to web development; but I've created a webpage/game in Silverlight. I'm
I am new to web development, and I am working on a basic image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.