这个ajax代码中的错误是什么
What is the error inside in this ajax code?
下面是我使用Ajax从数据库中检查用户名的代码。html页面中的输入文本不会进入checkusername.php $_POST['uname'];
可能是什么错误?我试了所有的东西,但都不起作用。单击按钮时,如何将用户名文本框中的值带入checkusername.php文件?
html页面代码
<input type="text" name="uname" class="form-control" id="username" placeholder="UserName (0-9,A-Z,a-z)">
<input type='button' id='check_username_availability' value='Check Username Availability'>
<div id='username_availability_result'></div>
checkusername.php
<?php
$con=mysqli_connect("localhost","root","","database")or die(mysqli_error());
//get the username
$username = mysqli_real_escape_string($con,$_POST['uname']);
//mysql query to select field username if it's equal to the username that we check '
$result =mysqli_query($con,"Select username from users where username='$username'") or die(mysqli_error($con));
//if number of rows fields is bigger them 0 that means it's NOT available '
if(mysqli_num_rows($result)>0){
//and we send 0 to the ajax request
echo 0;
}else{
//else if it's not bigger then 0, then it's available '
//and we send 1 to the ajax request
echo 1;
}
?>
//用于检查实时用户名可用性的脚本
<script>
$(document).ready(function() {
//the min chars for username
var min_chars = 6;
//result texts
var characters_error ='Minimum amount of chars is 6';
var checking_html = '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("checkusername.php",{ username: uname },
function(result){
//if the result is 1
if(result == 1){
//show that the username is available
$('#username_availability_result').html(username + ' is Available');
}else{
//show that the username is NOT available
$('#username_availability_result').html(username + ' is not Available');
}
});
}
</script>
交换值:
{ username: uname }
到此:
{ uname: username }
当您发送username
时,php根据这个$_POST['uname']
期望一个名为uname
的var
因此,简单的更改就是交换对象中的属性。
简单的打字错误。
更改
var username = $('#username').val();
//use ajax to run the check
$.post("checkusername.php",{ username: uname },
至
var username = $('#username').val();
//use ajax to run the check
$.post("checkusername.php",{ uname: username },// Observe username.
您正在发布到checkusername.php{username: uname}
尝试获取$_POST['uname']
,而在js中没有uname
变量。将{ username: uname }
更改为{ uname: username }
相关文章:
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- 处理浮点错误的最佳方法是什么
- 这个JS代码中的错误是什么
- 角度中的这个错误是什么
- ng路由不起作用.我不知道错误是什么,因为控制台中没有显示任何内容
- 这个ajax代码中的错误是什么
- win.js.ui.html控件:'元素'未定义.?这个错误是什么
- 这个语法错误是什么?(测试Hapi.js)
- JSP内部的Java代码中的错误是什么
- JavaScript中数组的错误是什么?
- 函数在传递数据给PHP文件时没有定义,错误是什么?
- 这个错误是什么"[$compile:nonassign]"在angularjs指令中
- 这个对象合并函数中可能出现的错误是什么?
- JavaScript代码中的错误是什么
- 这个AJAX jQuery的错误是什么?
- Jquery代码不起作用,但JavaScript起作用.我的错误是什么
- JSLint错误“是什么;类型错误"意思是
- 我的代码中的错误是什么?变量的乘法没有显示在总数中
- 当openFaces组件中的validationError=true时,具体的验证错误是什么?
- 当我试图通过Ajax传递值到php文件时,我的代码中的错误是什么