使用javascript按钮点击更改php状态的Ajax

I am trying to display 9 last uploaded images through AJAX, but also be able to navigate through past uploaded images as well.

 <script language = "javascript" type = "text/javascript">
              var x = 0;
             var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
            nextButton.onclick = function(){
                  x+=1;
                      };

         document.getElementById("tst").innerHTML =         xmlhttp.responseText;

    };

           xmlhttp.open("GET", "getImage.php?=" + x , true);
           xmlhttp.send();

      </script>

             #getImage.php file
                 <?php 
                  $q = $_REQUEST["q"];
                  $req = intval($q);
                  echo $req;
                  ?>

I need $req to update every time the button is pressed but also be 0 when the page is loaded so it does not only display the images when the button pressed but also when the page has just loaded Thanks

xmlhttp.open("GET", "getImage.php?=" + x , true);

is wrong as there is no query parameter q replace it with

xmlhttp.open("GET", "getImage.php?q=" + x , true);

hope it helps :)

The value of q in not passing to php file. Add query string parameter q Just replace this line:

xmlhttp.open("GET", "getImage.php?=" + x , true);

with this:

xmlhttp.open("GET", "getImage.php?q=" + x , true);