我在网站上创建了一个注册页面,我目前正在尝试实现一个AJAX用户名检查功能。我的问题是,当在输入字段中输入用户名时,它根本不运行。
任何帮助都会感激不尽。
注册页面
<html>
<head>
<title>Sign up Fabio's</title>
<link rel="icon" href="Images/Pizza.ico">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link href="mystyle.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.9.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#Username").keyup(function (e) {
var username = $(this).val();
$.post('usernamecheck.php', {'username':username}, function(data) {
$("#user-result").html(data);
}
});
});
</script>
</head>
<body>
<div id="Content2">
<form action="newuser.php" class="form" method="post" onsubmit="return validateForm()" >
<h1>Sign Up</h1>
<label for="Title">Title </label><br>
<select name="Title" >
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
<option value="Miss">Miss</option>
</select></br>
<label for="FirstName">First name</label> <input type="text" name="FirstName" required></br>
<label for ="LastName">Last name</label><input type="text" name="LastName" required></br>
<label for="Username">Username</label> <input type="text" name="Username" required><span id ="user-result"></span>
<label for="Email">Email</label> <input type="email" name="Email" required>
<label for ="Password1">Password</label><input type="password" name="Password1" id ="Password1" required></br>
<label for="Password2">Confirm Password </label><input type="password" name="Password2" id="Password2" required></br>
<input type="submit" value="Sign Up">
</form>
</div>
</body>
</html>
usernamecheck.php
<?php
if(isset($_POST["username"]))
{
include 'dbconnect.php';
$connection = mysqli_connect($db_host, $db_username, $db_password, $db_database);
// Check connection
if (mysqli_connect_errno($connection)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$username = $_POST["username"];
$result = mysqli_query($connection, "SELECT Id From Userv2 WHERE Username = '$username'");
$username_true = mysqli_num_rows($result);
if($username_true >0){
echo "Sorry $username has been taken";
}
else{
echo "$username is available";
}
mysqli_close($connection);
}
答案 0 :(得分:1)
首先:keyUp未分配给输入字段,请确保username的输入字段ID为id =&#34; Username&#34;
也请尝试以下代码。我添加了一个控制台日志,以查看是否将到达并执行post函数。如果在控制台中找到帖子,请告诉我
<script type="text/javascript">
$(document).ready(function() {
$("#Username").keyup(function (e) {
var username = $(this).val();
$.post('usernamecheck.php', {'username':username}, function(data) {
$("#user-result").html(data);
console.log('the post is done');
}
});
});
</script>
答案 1 :(得分:0)
您没有关闭$ .post()。试试:
<script type="text/javascript">
$(document).ready(function() {
$("#Username").keyup(function (e) {
var username = $(this).val();
$.post('usernamecheck.php', {'username':username}, function(data) {
$("#user-result").html(data);
}); // closes $.post()
});
});
</script>
警告:您在usernamecheck.php中遇到SQL注入错误:
$username = $_POST["username"];
替换为:
$username = mysqli_real_escape_string($connection, $_POST["username"]);