简体   繁体   English

Ajax用javascript按钮更改php状态

[英]Ajax changing php state with javascript button click

I am trying to display 9 last uploaded images through AJAX, but also be able to navigate through past uploaded images as well. 我正在尝试通过AJAX显示9个最后上传的图像,但是也能够浏览过去的上传图像。

 <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 我需要$ req每次按下按钮时进行更新,但在页面加载时也为0,因此它不仅在按下按钮时显示图像,而且还在页面刚刚加载时显示图像。

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

is wrong as there is no query parameter q replace it with 错误,因为没有查询参数q替换为

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

hope it helps :) 希望能帮助到你 :)

The value of q in not passing to php file. q的值不传递给php文件。 Add query string parameter q Just replace this line: 添加查询字符串参数q只需替换此行:

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

with this: 有了这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM