I have a simple form for submitting an email address to a database. I want to check first, using javascript and AJAX, if that email address already exists in the table. The code I am using always returns true regardless if the row already exists in the database. Any hints?
JS Code
<script>
$('form').submit(function(e)
{
alert('submit intercepted');
e.preventDefault(e);
var u = $('#email').val();
$.ajax({
url: './test2.php',
type: 'POST',
data: u,
success: function (response) {
//get response from your php page (what you echo or print)
$('#status').append('<p>The email is ok to use and the row has been inserted </p><p>' + response);
console.log('Submitted');
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
alert('Email exists');
}
});
});
</script>
HTML Code
<form id="form" action="./test2.php" method="post">
E-mail: <input type="email" name="email" id="email" required><br>
<span id="status"> </span>
<input type="submit" id="btnsub">
</form>
PHP Code
if(isset($email)) {
$sql_email_check = $conn->query("SELECT Email FROM test WHERE Email='$email' LIMIT 1") ;
$email_check = $sql_email_check->num_rows;
if ($email_check < 1) {
//return "Available";
return true;
//header('Location: /test.php');
exit();
}
else
{
//return "Unavailable";
return false;
//header('Location: /test.php');
exit();
}
}
Create ajaxfile. php file to handle AJAX requests. Check $_POST['username'] value exists in the username field in users table. If it exists then return Available.
AJAX stands for Asynchronous JavaScript and XML. It is used for allowing the client side of an application to communitcate with the server side of the application. Before AJAX, there was no way for the client side of a web application to communicate directly with the server. Instead, you would have to use page loads.
ajax returns a promise object, so that we don't have to pass a callback function.
So basically the problem was returning the answer from the php code to the ajax function. for some reason the return values were not interpreted correctly as they were expected (from my understanding anyway)
PHP CODE
if(isset($email)) {
$sql_email_check = $conn->query("SELECT Email FROM test WHERE Email='$email' LIMIT 1") ;
$email_check = $sql_email_check->num_rows;
if ($email_check == 0) {
echo 'true';
return;
}
else
{
echo 'false';
return;
}
}
JS CODE
<script>
$('form').submit(function(e)
{
alert('submit intercepted');
e.preventDefault(e);
var u = { 'email': $('#email').val() };
$.ajax({
url: 'test2.php',
type: 'POST',
//dataType: 'string',
data: u,
success: function (response) {
//get response from your php page (what you echo or print)
console.log(response);
if (response === 'true') {
$('#status').append('<p>The email is ok to use and the row has been inserted </p><p>' + response);
} else {
console.log('Email does exist ');
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With