Form validation with unique email address check doesn't work
Hi, I'm enough new to JQUERY and would need some help in order to fix a form validation.
I would validate the email address is unique once the user submit the form.
In the code I developed, I receive always the message error, saying the email address in already in use, even if this is not true.
Here is my code :
JS CODE :
$().ready(function() {
$.validator.addMethod("verifyEmail",
function(value, element) {
var result = false;
$.ajax({
type:"POST",
async: false,
url: "user_unique_validator.php", // script to validate in server side
data: {"doc-email": value},
success: function(data) {
result = (data == true) ? true : false;
}
});
// return true if username is exist in database
return result;
alert("RESULT "+result);
},
"Utente giĆ in uso!"
);
$("#form-doctor").validate({
rules: {
"doc-email": {
required: true,
verifyEmail: true
}
}
});
// validate signup form on keyup and submit
$("#form-doctor").submit(function( event ) {
event.preventDefault();
if($('#form-doctor').valid()) {
$form = $('#form-doctor');
data_form = new FormData($form[0]);
$.ajax({
type: "POST",
processData: false,
contentType: false,
url: "ajax/ajax_doctor_modificar.php",
data: data_form,
success: function(data) {
window.location.replace($('#lang').val()+'/doctor/'+data+'/');
}
});
}
});
});
PHP CODE :
<?php
require_once dirname(__FILE__) . '/ajax/login_check.php';
require_once dirname(__FILE__) . '/connections/conexion.php';
$searchVal = $_POST['doc-email'];
$myfile = fopen("testfile.txt", "w") ;
try {
fwrite($myfile,"ERROR FOR :".$searchVal);
$stmt = $db->prepare("SELECT email FROM doctores where email = '$searchVal'");
$stmt->execute();
$result = $stmt->rowCount();
fwrite($myfile,"Result :".$result);
if($result>0){
$result = true;
}else {
$result = false;
}
return $result;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
fclose($myfile);
?>
HTML :
<div class="form-group col-sm-6">
<label class="control-label"><?php echo $array['email']?></label>
<input name="doc-email" class="form-control required" type="text" placeholder="<?php echo $palabra['array']?>" value="<?php echo $doctores->email?>">
</div><!-- /form-group -->