我想#username_input
根据该 php 脚本返回的值显示。似乎 if 条件不起作用。
JavaScript:
$(document).ready(function () {
$('#username_input').keyup(function () {
$.post('header/check_username.php', {
username: form.username.value
},
function (result_username) {
if (result_username == 'a') {
$('#feedback_username').html('choose a username').show();
}
if (result_username == 'b') {
$('#feedback_username').html('Too short').show();
}
});
});
HTML:
<form id="registeration_form" name="form" onsubmit="return hello()" >
<table>
<tr>
<td>Username </td>
<td><input type="text" id="username_input" name="username" autofucus/></td>
<td><span id="feedback_username"></span></td>
<br/><br/>
<tr>
<td>Password </td>
<td><input type="password" id="password_input" name="password"/></td>
</tr>
<br/><br/>
<tr>
<td>Confirm Password</td>
<td><input type="password" id="password_confirm" name="password_confirm"/></td>
<td><span id="feedback_password"></span></td>
</tr>
<br/><br/>
<tr>
<td>Email </td>
<td><input type="text" id= "email_input" name="email" /></td>
</tr>
</table>
<center><input type="submit" x value="Register"/></center>
<br/>
<div id= "onsubmit_feedback"></div>
<center><a href="javascript:register('hide');">close</a></center>
</form>
a
如果用户名为空且b
用户名少于 6 个字符,则返回。
PHP:
<?php
$con = mysqli_connect("localhost:3306", "root", "", "project_new");
if (mysqli_connect_errno($con)) {
echo "Unsuccessful" . mysqli_connect_error();
exit();
}
@$username = mysqli_real_escape_string($con, trim($_POST['username']));
$check = mysqli_query($con, "select u from users where u ='$username'");
$check_num_rows = mysqli_num_rows($check);
if ($username == NULL)
retutn 'a';
//echo "choose a username";
else if (strlen($username) <= 5)
return 'b';
//echo "Too Short. Minimum 6 characters";
else {
if ($check_num_rows == 0)
echo "Available :)";
else if ($check_num_rows == 1)
echo "Not Available :( ";
}
?>