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. This is the 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:
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.
$_POST
is for key/value formatted, URL encoded data. You're sending raw JSON which is different. With your current Javascript, you would need to use $HTTP_RAW_POST_DATA
to get the JSON on the PHP side.
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) data
object. On the PHP side, your JSON will be decoded to an object, so use:
$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()
does not update the variable passed as an argument, you must capture the return value.
With the jQuery.ajax method, the data
parameter is supposed to be a JSON object. 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']
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.