[英]jquery val() and serialize()
Problem: When i use .serialize(), it works fine as I want . 问题:当我使用.serialize()时,它可以按我的要求正常工作。 When i use .val(), it shows on console: 当我使用.val()时,它在控制台上显示:
failed
admin
admin is username, but jquery does not get echo from php i think admin是用户名,但jquery不会从PHP中获取回声,我认为
Jquery file jQuery文件
$(document).ready(function() {
$('#username').blur(function(event) {
// var submitData = $('#login_form').serialize();
var username=$("#username").val();
$.ajax({
url: './checkUsername.php',
type: 'POST',
data: username
})
.done(function(res) {
if (res == 1) {
$('#checkUsername').html("User exists");
console.log("success");
} else {
$('#checkUsername').html("User do not exists");
console.log("failed");
console.log(username);
}
})});
Php file php文件
<?php
require_once 'config.php';
$username = $_POST['username'];
$query = "select * from members where username='$username'";
if ($stmt = mysqli_prepare($con, $query)) {
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
if (mysqli_stmt_num_rows($stmt) == 1) {
echo 1;
} else {
echo 0;
}
mysqli_stmt_close($stmt);
}
mysqli_close($con);
?>
You're only sending a value: 您只发送一个值:
data: username
But the server-side code expects a key/value pair : 但是服务器端代码需要键/值对 :
$_POST['username']
Send an object with a key for that value: 发送带有该值的键的对象:
data: { username: username }
change data: username
to {username: username}
将data: username
更改为{username: username}
as passing of data requires an index. 因为数据传递需要索引。
know more about jquery post http://api.jquery.com/jquery.post/ 了解有关jquery帖子的更多信息http://api.jquery.com/jquery.post/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.