1

I will break this question into paragraphs for easier reference.

  1. I have a script that calls a php file using Ajax; the script is executed once a button is pressed.

  2. The PHP file contains a simple code that plusses a number with 1.

    <?php 
    $response = $response + 1;
    echo $response;
    ?>
    
  3. However, something goes wrong when the button is pressed multiple times. I have done some research and I can conclude that there is either something wrong with the simple PHP script that I have made, or the code is only executed once per page reload.

  4. I read an article explaining a lot of work using Javascript if I want an Ajax command to execute multiple times during the same script but I don't see the reason that it can't execute multiple times already when the button is pressed.

    Question: Is the PHP file "post.php" even getting executed everytime the button is pressed? Is it something with my PHP file? How can I make the script plus the number by 1 everytime the button is pressed? And is it because I need some extra Javascript to do that?

The rest of the script is here:

 <head>
        <script>
        function loadXMLDoc()
        {
        var xmlhttp;
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            }
          }
        xmlhttp.open("POST","post.php",true);
        xmlhttp.send();
        }
    </script>

</head>
<body>
    <p>This button will execute an Ajax function</p> 
    <button type="button" onclick="loadXMLDoc()">Press here!</button>
    <div id="myDiv"></div>
    <script type="text/javascript" src="http://www.parameter.dk/counter/5b0d9873158158e08cad4c71256eb27c"></script>
</body>

If I wasn't clear enough at some point, please pick out the paragraph number and I'll deepen that part.

2
  • 1
    What is $response? What makes you think it will get increased by 1 when you access the page multiple times? It will be re-initialized every time you send an HTTP request. Your problem is that you need it to persist. Commented Mar 30, 2013 at 16:53
  • $response is the variable that I want to increase by 1 everytime the button is pressed. Wont the $response keep its value when the website is reloaded and be plussed by 1 everytime the button is pressed? Commented Mar 30, 2013 at 16:55

1 Answer 1

1

In your code $response is always 1 because php by default have no state. You can fix it by saving response inside a session.

session_start();
if (isset($_SESSION['response'])) {
    $_SESSION['response'] = $_SESSION['response'] + 1;
} else {
    $_SESSION['response'] = 1;
}
echo $_SESSION['response'];
Sign up to request clarification or add additional context in comments.

1 Comment

So the variable is now remembered by the users session? The reason that my old script couldn't handle it was because the whole script restarts everytime the button is pressed or am I missing something? This script works however, thanks for the quick solution

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.