jQuery validate email address against db when focus is moved

Hello Experts,

I am looking for someone to point me in the right direction with this scenerio.
What I have is a register user form where the user types in their email address.
As soon as they tab/move away from the email address text box, I want some jQuery to compare what the user has typed against my db (by calling some php code and passing in their email address). The php would return true or false (or 0 or 1 etc) - which the jQuery would then display as a success or failure (using the relevant icons).

Does this make sense?

Many Thanks
jagkuAsked:
Who is Participating?
 
moagriusConnect With a Mentor Commented:
here's one way...  i assume you already have the php, but a basic sample is included as well
// assuming your input has the name 'email' and there's only one
// this will send one POST variable named 'email' to your php script
$('input[name="email"]').blur(function(e){
  $.ajax({
    url : 'the/path/to/your/script.php',
    type : 'POST',
    data : {
      email : $(this).val()
    },
    success : function(returns){
      if(returns == '1') {
        // this would mean the php script output '1' for true - react here
      } else {
        // this would mean the php script output anything but '1' - false - react here
      }
    }
  });
});

// the php might look like this
$email = $_POST['email'] or die('No email specified');
$conn = mysql_connect('localhost', 'user', 'pass');
$link = mysql_select_db('dbname', $conn);
$email = mysql_real_escape_string($email);
$result = mysql_query("select email from emails where email='{$email}' limit 1");
if($result && mysql_num_rows($result) > 0)) echo '1';
echo '0';

Open in new window

0
 
moagriusCommented:
forgot an 'else' at the end of the php...  all the the above is untested and might have syntax errors but should generally demonstrate the idea, which is about as much as is possible without details regarding the db, the selectors of the input, etc...
0
 
pius_babbunCommented:
I suggest you can go in for ajax form submission

http://mazesolutions.me/ajax/ajax-login-form-php-javascript

http://mazesolutions.me/demos/ajax2/

just check if this is helpful to you !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.