我尝试创建一个表单来检查用户是否存在于数据库中,但我在将 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);
?>