我正在开发一个php / MySQL项目,要求我在用户输入用户名时实时检查用户名。
这是我的 username.php ,其中用户实际输入了用户名&密码以及触发 check.php 的地方......
<html>
<head>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#username").change(function(){
$("#message").html("<img src='images/loader.gif' /> checking...");
var username = $("#username").val();
$.ajax({
type:"post",
url:"check.php",
data:"username =" + username,
success:function(data){
if(data==0){
$("#message").html("<img src='images/tick.png' /><span style='font-size:13px; color: black'> Username available</span>");
}
else{
$("#message").html("<img src='images/err.png' /><span style=font-size:13px; color: red'> Username already taken</span>");
}
}
});
});
});
</script>
</head>
<body>
<table>
<tr>
<td>Username</td>
<td>:</td>
<td><input type="text" name="username" id="username"/><td>
<td id="message"><td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="text" name="password" id="password" /><td>
</tr>
</table>
</body>
</html>
这是 check.php ,其中在数据库中检查用户名。
<?php
$con = mysqli_connect("localhost", "root", "hilbi", "userdb");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// $username = $_POST["username"];
$username = 'hilbi';
$query = mysqli_query($con,"SELECT * FROM users WHERE username = '$username' ");
$find = mysqli_num_rows($query);
echo $find;
mysqli_close($con);
?>
现在 check.php 工作正常(我通过单独执行检查,然后返回 1 找到用户名,反之亦然)
但是,当我执行 username.php 时,它始终会返回已使用的用户名 。看起来它实际上并没有在数据库中看到实际上没有这样的用户名。
任何形式的帮助将不胜感激......
答案 0 :(得分:5)
使用jQuery在javascript中发送数据:
//some basic validation
if(!isset($_POST['user'] || empty($_POST['user']))
{
echo '0'; exit();
}
$username = trim($_POST['user']);
在你的check.php中获取这样的用户名
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
[queuedToDoArray moveObjectAtIndex:fromIndexPath.row toIndex:toIndexPath.row];
NSIndexPath *lowerIndexPath = (fromIndexPath.row < toIndexPath.row ? fromIndexPath : toIndexPath);
NSIndexPath *higherIndexPath = (fromIndexPath.row > toIndexPath.row ? fromIndexPath : toIndexPath);
//Update all the queue numbers in between moved indexes
for (int i = lowerIndexPath.row; i <= higherIndexPath.row; i++) {
NSIndexPath *currentIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
CustomCell *currentCell = [todoTable cellForRowAtIndexPath:currentIndexPath];
[currentCell.queueNumber setText:[NSString stringWithFormat:@"%i", currentIndexPath.row + 1]];
}
}
为了使其正常工作,check.php必须返回1或0,不要在那里或其他任何地回显mysql错误。如果发生任何错误,只需回显0。
答案 1 :(得分:0)
您需要正确传递$ username变量。
单引号php变量被视为普通字符串,变量不被评估。
$query = mysqli_query($con,"SELECT * FROM users WHERE username = " . $username . ";");
OR
$query = mysqli_query($con,"SELECT * FROM users WHERE username = {$username}");
答案 2 :(得分:0)
data:"username =" + username
data:"username=" + username
您必须删除用户名和所有工作后的空格
答案 3 :(得分:-1)
我花了一些时间寻找和测试答案,所以我应该发布我的结果以及对我有用的东西。
html, or php form:
<script src="http://code.jquery.com/jquery-3.3.1.js"></script>
<form class="a16" action="register.php" method="POST" autocomplete="on">
<label for="userName" class=" ">Użytkownik:</label>
<input type="text" name="userName" placeholder="Username" id="userName1" onBlur="checkAvailability()">
<span id="user-availability-status"></span>
<p><img src="LoaderIcon.gif" id="loaderIcon" style="display:none" /></p>
<script type="text/javascript">
function checkAvailability() {
$("#loaderIcon").show();
jQuery.ajax({
url: "proceser.php",
data:'userName='+$("#userName1").val(),
type: "POST",
success:function(data){
$("#user-availability-status").html(data);
$("#loaderIcon").hide();
},
error:function (){}
});
}
</script>
and proceser.php :
<?php
include 'dbconnect.php'; //standard datebase local connection..
if(isset($_POST['userName']) && $_POST['userName']!="") {
if ($stmt = $con->prepare('SELECT userName FROM users WHERE userName = ?')) {
$stmt->bind_param('s', $_POST['userName']);
$stmt->execute();
$stmt->store_result();
$numRows = $stmt->num_rows;
if ($numRows > 0) {
echo "<span class=''> Username Not Available.</span>";
} else {
echo "<span class=''> Username Available.</span>";
}
}
}
$con->close();
ob_end_flush();
?>
Thank you!:)