Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax使用javascript按钮更改php状态单击_Javascript_Php_Html_Ajax_Button - Fatal编程技术网

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

Ajax使用javascript按钮更改php状态单击,javascript,php,html,ajax,button,Javascript,Php,Html,Ajax,Button,我试图通过AJAX显示最近上传的9张图片,但也能够浏览过去上传的图片 <script language = "javascript" type = "text/javascript"> var x = 0; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { nextButton

我试图通过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;
                  ?>

var x=0;
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
nextButton.onclick=函数(){
x+=1;
};
document.getElementById(“tst”).innerHTML=xmlhttp.responseText;
};
open(“GET”、“getImage.php?=”+x,true);
xmlhttp.send();
#getImage.php文件
我需要$req在每次按下按钮时更新,但在加载页面时也需要$req为0,这样它不仅在按下按钮时显示图像,而且在页面刚加载时也显示图像 谢谢

是错误的,因为没有查询参数q将其替换为

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

希望有帮助:)

在不传递到php文件时
q
的值。添加查询字符串参数
q
只需更换这一行:

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

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

主要问题是,当我单击按钮时,$req没有更新。我确实更改了它,但它仍然不工作。当我将其更改为:if(x==0){xmlhttp.open(“GET”,“getImage.php?q=”,true);xmlhttp.send();}else{nextButton.onclick=function(){x+=1;xmlhttp.open(“GET”,“getImage.php?q=”+x,true);xmlhttp.send();}
xmlhttp.open("GET", "getImage.php?q=" + x , true);