我希望能够通过将文本框数据发布到 php 来检查用户 eamil 是否存在于使用 AJAX 的“blur()”上,我可以看到使用 firebug 时发布的数据,但我不断收到此错误:未定义索引:数据,我已经很努力了,但还没有解决这个问题。这是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!");
}
}
});
});
});
这是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);
}
我添加了“if (isset($_POST['data']))”来检查帖子数据是否已设置,但显然还没有。