我尝试创建一个表单来检查用户是否存在于数据库中,但是我在连接AJAX和sql时遇到问题,如果你能帮我找到这个问题我会非常感激,抱歉我的英语不是很好。这些是我的代码:
<!DOCTTYPE html>
<html lang="en">
<head>
<mete charset="UTF-8">
<title>Email client</title>
<script type="text/javascript">
function load(){
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200 ){
document.getElementById('info').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'checkuser.php', true);
xmlhttp.send();
}
</script>
</head>
<body>
<form name="loginform" onclick="load();">
Username : <input name="userID" type="text" id="userID"><br />
Password : <input name="password" type="password" id="passWD"><br />
<input type="submit" id="button" value="Get in there"> <br />
<p>Don't have an account? <a href="register.html"> please register </a></p>
</form>
<div id="info"></div>
</body>
</html>
我的php(checkuser.php):
<?php
$dbhost = 'localhost';
$dbuser = 'xxxx';
$dbpass = 'xxxx';
$dbname = 'xxxx';
$dbtable = 'xxxx';
$q=$_GET["userID"];
$p=$_GET["passWD"];
$con = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$dbselect = mysql_select_db($dbname,$con);
$sql="SELECT * FROM $dbtable WHERE userID='$q'";
$result = mysql_query($sql);
if (mysql_num_rows($result)==0) {
echo "not registered";
} else {
while($row = mysql_fetch_array($result))
{
if (($row['passWD'])==$p) {
echo "registered";
} else { echo "not registered";}
}
}
mysql_close($con);
?>
答案 0 :(得分:1)
您的PHP期望传递两个查询字符串参数(不要在查询字符串中发送密码,它们可能会被记录,使用POST请求)但您的JavaScript只是请求脚本的URL而不是查询字符串。
据推测,您希望扩展load
以从表单中获取数据,并在load
事件中为表单而不是submit
事件调用load
文档。