我在运行以下脚本时遇到问题。它除了AJAX部分以外正确运行。它不会调用URL“check_username.php”。我检查了数据库连接。与数据库没有连接
任何帮助将不胜感激。 感谢
jQuery代码 -
<script type="text/javascript">
$(document).ready(function() {
//the min chars for username
var min_chars = 3;
//result texts
var characters_error = 'Minimum amount of chars is 3';
var checking_html = '<img src="images/loading.gif" /> Checking...';
//when button is clicked
$('#check_username_availability').click(function(){
//run the character number check
if($('#username').val().length < min_chars){
//if it's bellow the minimum show characters_error text
$('#username_availability_result').html(characters_error);
}else{
//else show the cheking_text and run the function to check
$('#username_availability_result').html(checking_html);
check_availability();
}
});
});
//function to check username availability
function check_availability(){
//get the username
var username = $('#username').val();
//use ajax to run the check
$.post("check_username.php", { username: username }, //This part is not being executed .
function(result){
//if the result is 1
if(result == 1){
//show that the username is available
$('#username_availability_result').html('<span class="is_available"><b>' +username + '</b> is Available</span>');
}else{
//show that the username is NOT available
$('#username_availability_result').html('<span class="is_not_available"><b>' +username + '</b> is not Available</span>');
}
});
}
</script>`
check_username.php -
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('modal');
$username = mysql_real_escape_string($_POST['username']);
$result = mysql_query("SELECT username FROM users WHERE username=`$username`");
$num_rows = mysql_num_rows($result);
//if number of rows fields is bigger them 0 that means it's NOT available '
if($numrows>0) {
echo 0;
}else{
echo 1;
}
?>
控制台中有任何错误? – leonardeveloper
使用您选择的浏览器中的开发人员控制台查看服务器对$ .post请求的实际响应是什么,以及提供了哪些参数。您的SQL查询是错误的,因为它会检索表中的所有用户,而不仅仅是您要查找的用户名。 – MatsLindh
在控制台中没有错误 –