我正试图将参数从AJAX发送到PHP,但这并没有发生,我不知道为什么。这是一个简单的代码。我只想发送参数param
到相同的表单,并将值打印到页面。
源代码
PHP:
<?php
if(isset($_GET))
{
echo $_GET["param"];
}
?>
HTML:
<table>
<tr>
<td><input type="text" id="myText" /></td>
<td style="width:35px;"><div id='myDiv' style=" display:none;"><img src="30.gif" id="myImage" /></div></td>
<td><input type="button" value="button" name="myButton" onClick="myFunction()"/></td>
</tr>
</table>
JavaScript:
<script>
function myFunction(){
document.getElementById('myDiv').style.display='inline';
xmlHttp=new XMLHttpRequest();
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200)
{
setTimeout("callMe()",3000);
}
}
xmlHttp.open("GET","myAjax.php?param=1",true);
xmlHttp.send();
}
function callMe()
{
document.getElementById('myText').value=xmlHttp.responseText;
document.getElementById('myDiv').style.display='none';
}
</script>
只需尝试:
if (isset($_GET['param'])) {
echo $_GET['param'];
}
经过测试后效果很好。