Ajax使用javascript按钮更改php状态单击
我试图通过AJAX显示最近上传的9张图片,但也能够浏览过去上传的图片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
<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);