After getting help from @juhana (thank you again) I ended up with theese codes to validate email input:
validate email:
function validateEmail(){
var a = $("#email").val();
$.ajax({
type: "POST",
url: "check_email.php",
data: "email="+a,
success: function(rsp){
//if it's valid email
if(rsp == "ok"){
email.removeClass("error");
emailInfo.text("");
emailInfo.removeClass("error");
return true;
}
else
//if it exists
if(rsp == "exists" ){
email.addClass("error");
emailInfo.text("E-mail already in use");
emailInfo.addClass("error");
return false;
}
else
//if it's NOT valid
if(rsp == "invalid"){
email.addClass("error");
emailInfo.text("Please type a valid E-mail");
emailInfo.addClass("error");
return false;
}
}
});
}
check_email.php
<?php
require_once('db_conn.php');
require_once('is_email.php');
$email = mysql_real_escape_string($_POST['email']);
if (is_email($_POST['email'])){
echo 'ok';
$checkemail = mysql_query("SELECT E_mail FROM orders WHERE E_mail='$email'");
$email_exist = mysql_num_rows($checkemail);
if($email_exist>0){
echo 'exists';
}
}else{
echo 'invalid';
}
?>
Now 2 out of 3 are working the “ok” and the “invalid” ones… the “exists” doesn’t.
What’s wrong here???
Thank you
by reading your code above, if the email was valid, but existed in the database, it would return “okexists” which would cause your javascript to fail. You’d need to change it to something like this:
This allows it to return only the token “exists” when its a valid, but existing email. And only the token “ok” when its a valid, not previously existing email. And of course it returns invalid if it doesn’t pass the
is_email()test.