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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

moagriusCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.