HTML Code for the form submission:
<div class="control-group">
<label class="control-label" for="input01">Email</label>
<div class="controls">
<input type="text" class="input-xlarge" id="email" name="email"
rel="popover" data-content="What’s your email address?"
data-original-title="Email">
<span class="check" style="color:red;" ></span>
</div>
</div>
jQuery Code to post the email into the PHP script:
$(function () {
$('#email').keyup(function () {
var email = $(this).val();
if (email != '') {
//$('.check').show();
$('error').fadeIn(400).html('<img src="/img/ajax-loading.gif" />');
var dataString = 'email=' + email;
$.ajax({
type: "POST",
url: "pages/check_email.php",
data: dataString,
cache: false,
success: function (result) {
if (result == '') {
$('.check').html(email + ' Avaliable');
$('#create').attr('disabled', '');
$('#create').attr('value', 'Active');
$("#email").removeClass("red");
$("#email").addClass("white");
} else {
$('.check').html(email + ' ' + result);
$('#create').attr('disabled', 'disabled');
$('#create').attr('value', 'Deactive');
$("#email").removeClass("white");
$("#email").addClass("red");
}
}
});
} else {
$('.check').html('');
$('#submit').attr('disabled', 'disabled');
$('#submit').attr('value', 'Deactive');
}
});
});
PHP Code to check the email availability and return 1 or 0 should a value be found/not found:
include_once "../base.php";
$stmt = $db->prepare("SELECT * FROM `Consultants` WHERE email =':email'");
$stmt->bindParam(":email", $_POST['email']);
$stmt->execute();
$num_rows = $stmt->fetchColumn();
$HTML='';
if($num_rows > 0){
$HTML='is already used';
}else{
$HTML='';
}
echo $HTML;
$stmt = null;
For every email that’s inputted, it returns ‘available’. I have several emails within my database that I’m using. I receive no errors in the console neither.
You select all fields from your
Consultantstable, so the first column probably evaluates to something bigger than 0.Additionally, your prepared statement shouldn’t use quotes around the bound parameter
:email.To fix: