我有一个提交按钮。点击它,我试着从php回调文件返回一个响应。但我没有得到任何回应。点击提交后,我会收到以下网址
http://localhost/folder/new.php?submit=Submit+Query
PHP文件命名为new.php
<script language="javascript" type="text/javascript">
function callme()
{
var req;
try
{
req = new XMLHttpRequest();
}catch (e)
{
try
{
req = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e)
{
try
{
req = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e)
{
alert("Your browser broke!");
return false;
}
}
}
req.onreadystatechange=function(){
if(req.readyState==4)
{
document.getElementById("display_area").innerHTML=req.responseText;;
}
}
req.open("GET","phpcall.php",true);
req.send();
}
</script>
和PHP回调文件 - 名为phpcall.php
<?php
echo "returning from phpcall.php";
?>
这可能是什么错误?网址更改为
的原因是什么http://localhost/folder/new.php?submit=Submit+Query
在表单部分添加 -
<form name='myForm'>
Max Age: <input type='text' id='age' /> <br />
Max WPM: <input type='text' id='wpm' />
<br />
Sex: <select id='sex'>
<option value="m">m</option>
<option value="f">f</option>
</select>
<input type='button' onclick='ajaxFunction()'
value='Query MySQL'/>
</form>
<div id='ajaxDiv'>Your result will display here</div>
答案 0 :(得分:1)
更改type="button"
按钮<input type="button" value="Submit">
。保持type="submit"
将提交表单,因此页面将刷新
答案 1 :(得分:1)
当您使用提交按钮和表单时,您的onclick处理程序必须返回false,或调用preventDefault方法。如果没有,您的表单将在XMLHttpRequest完成之前提交。