【发布时间】:2014-02-16 15:31:27
【问题描述】:
我是 AJAX 和 PHP 编程的新手。 我相信这很简单,但我很难。
我需要的是用户选择一个日期(使用 jquery 日期选择器)并单击“确定”按钮。
表单将调用页面 query.php(通过 Ajax)。 query.php 将检查日期。 如果日期在数据库中有注册表,则 query.php 将返回为 index.php 填充的表。
我的脚本基于这个例子:http://www.w3schools.com/php/php_ajax_database.asp
但在该示例中,触发器是事件 (onchange="showUser(this.value)")。 我需要在提交表单后调用 AJAX 函数。
我使用 () 完成了该操作,但是当我尝试使用 $q = $_GET['q']; 获取日期时该值为空白。
Index.php(相关信息):
<!DOCTYPE html>
<html lang='pt-BR'>
<head>
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","checar_data.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form id="dateForm" onsubmit="showUser(); return false;" method="GET">
Data:
<input name="datepicker" id="datepicker" type="text" size="10" maxlength="8" />
<input name="OK" value="OK" type="submit" >
</form>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
查询.php:
<?php
$q = intval($_GET['q']);
echo "$q -> bla bla";
?>
我该如何解决这个问题?
【问题讨论】:
标签: php jquery ajax forms xmlhttprequest