[英]Not getting posted data using AJAX
i want to be able to check if a user eamil exists on "blur()" with AJAX by posting the textbox data to php, i can see the data posted when i use firebug but i keep getting this error: Undefined index: data, i've tried so hard but haven't been able to resolve this issue yet. 我希望能够通过将文本框数据发布到php来检查用户eamil是否在AJAX的“ blur()”上存在,我可以在使用Firebug时看到发布的数据,但我不断收到此错误:未定义的索引:数据,我已经尽力了,但是还不能解决这个问题。 This is the javascript: 这是javascript:
$(document).ready(function() {
$('#regEmail').blur(function() {
var Email = $('#regEmail').val();
var data = '{Email:' + Email + '}';
$.ajax({
type: "POST",
url: "register.php",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var result = response.exists;
if (result == true) {
alert('Email already exists!');
return;
}
else {
alert("Does not exist!");
}
}
});
});
});
And this is the php code: 这是PHP代码:
if (isset($_POST['data']))
{
$db = new mysqli("localhost", "xxxx", "xxxxxxx", "xxxxxxx");
$data = $_POST['data'];
json_decode($data);
$Email = $data->fetch_assoc();
$Email = $Email["Email"];
$sql = "SELECT COUNT(*) as isExisting FROM users WHERE user_email='$Email'";
$sql_result = $db->query($sql);
$result = $sql_result->fetch_assoc();
if ($result["isExisting"] > 0)
{
$response = json_encode(array('exists' => true));
} else {
$response = json_encode(array('exists' => false));
}
echo $response;
print json_encode($_POST);
}
I added "if (isset($_POST['data']))" to check if the post data has been set but apparently it hasn't. 我添加了“ if(isset($ _ POST ['data']))”,以检查是否已设置发布数据,但显然没有。
$_POST
is for key/value formatted, URL encoded data. $_POST
用于键/值格式的URL编码数据。 You're sending raw JSON which is different. 您正在发送原始JSON,这是不同的。 With your current Javascript, you would need to use $HTTP_RAW_POST_DATA
to get the JSON on the PHP side. 使用当前的Javascript,您需要使用$HTTP_RAW_POST_DATA
在PHP端获取JSON。
This will do it: 这样做:
var data = { Email : Email };
$.ajax({
type: "POST",
url: "register.php",
data: { data : JSON.stringify(data) },
dataType: "json",
success: function (response) {
var result = response.exists;
if (result == true) {
alert('Email already exists!');
return;
}
else {
alert("Does not exist!");
}
}
});
Changes: 变化:
data
variable to a Javascript object (not JSON) Javascript对象(不是JSON)的data
变量 data
object. 使用一个输入(数据)作为data
对象的JSON编码版本,以编码形式发送POST数据。 On the PHP side, your JSON will be decoded to an object, so use: 在PHP方面,您的JSON将被解码为一个对象,因此请使用:
$data = $_POST['data'];
$email = json_decode($data);
$Email = $Email->Email;
You can use the array syntax, but only if you set the second argument for json_decode()
to true
. 您可以使用数组语法,但json_decode()
的第二个参数设置为true
。 json_decode()
does not update the variable passed as an argument, you must capture the return value. json_decode()
不会更新作为参数传递的变量,必须捕获返回值。
With the jQuery.ajax method, the data
parameter is supposed to be a JSON object. 使用jQuery.ajax方法, data
参数应该是JSON对象。 You should write instead : 您应该改写:
var data = { 'Email': Email };
$.ajax({
type: "POST",
url: "register.php",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var result = response.exists;
if (result == true) {
alert('Email already exists!');
return;
}
else {
alert("Does not exist!");
}
}
}
);
And then you can use the email value server-side by accessing $_POST['Email']
. 然后,您可以通过访问$_POST['Email']
在服务器端使用电子邮件值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.