Check if email address exists in database using jQuery and PHP

I am trying to check the email address if it already exists in the table, but this does not work.

Here is my code:

<html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $('#Submit').click(function() { var emailVal = $('#email').val(); // assuming this is a input text field $.post('checkemail.php', {'email' : emailVal}, function(data) { alert(data); }); }); </script> </head> <body> <form id="form1" name="form1" method="post" action="view.php"> <p> <input type="text" name="email" id="email" /> </p> <p> <input type="submit" name="Submit" id="Submit" value="Submit" /> </p> </form> </body></html> 

When you click the submit button, it must first perform a check using jQuery and invoke the checkemail.php file, as shown below:

 <?php include("../includes/db.php"); $sql = "SELECT email FROM lf_clients WHERE email = " .$_POST['email']; $select = mysqli_query($con, $sql); $row = mysqli_fetch_assoc($select); if (mysqli_num_rows > 0) { echo "The email already exists."; } ?> 

However, when I click the submit button, it goes to view.php instead of checkemail.php. Before redirecting to view.php, it must first check if email exists or not. If the message already exists, it should not be redirected to view.php.

+4
source share
4 answers
 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function(){ //newly added $('#Submit').click(function() {alert('in'); var emailVal = $('#email').val(); // assuming this is a input text field $.post('checkemail.php', {'email' : emailVal}, function(data) { if(data=='exist') return false; else $('#form1').submit(); }); });}); </script> </head> <body> <form id="form1" name="form1" method="post" action="view.php"> <p> <input type="text" name="email" id="email" /> </p> <p> <input type="button" name="Submit" id="Submit" value="Submit" /> </p> </form> </body> </html> 

php code

 <?php include("../includes/db.php"); $sql = "SELECT email FROM lf_clients WHERE email = " .$_POST['email']; $select = mysqli_query($con, $sql); $row = mysqli_fetch_assoc($select); if (mysqli_num_rows > 0) { echo "exist"; }else echo 'notexist'; ?> 
+11
source

Best practice for this kind of thing is to use the jQuery validation method.

  $("#myform").validate({ rules: { email: { required: true, email: true, remote: "check-email.php" } }, messages:{ email:'Email address exists.' } }); 

In the PHP file, you can do what @Sachyn mentioned.

+1
source

I suspect you need to undo the standard action of the submit button. You may also need to change the action from a click in order to send, you can’t check it right now, though.

 $('#Submit').submit(function(e) { var usernameVal = $('#email').val(); // assuming this is a input text field $.post('checkemail.php', {'email' : emailVal}, function(data) { alert('data'); }); e.preventDefault(); // <------ }); 
0
source

You can do the following

  • In your PHP file -

      if (mysqli_num_rows > 0) { echo "0"; // email exists else echo "1"; // email doesn't exists return; 
  • In your HTML file use <input type="button" instead of <input type="submit"

And in JavaScript

 $.post('checkemail.php', {'email' : emailVal}, function(data) { if (data == 1) { $("#form1").submit(); } else { alert("Email already exists"); return false; } }); 
0
source

All Articles