how to not submit the form to the database with already existing email using jquery validation plugin ajax

1.6k views Asked by At

Hello everybody i am using jquery validation plugin. It's just working fine but my problem is if there is email exists a database. The form should not submitted until another valid email he choose. how to solve this problem.

The form is submitting even the email already exit in the database.

Here is form with ajax

<form id="ajax_form" action="form_action.php">
<label for="name">Name :</label>
  <input name="name" type="text"  required />

  <label for="name">Email :</label>
  <input name="email"  type="email" required />
  <input type="submit" value="Submit" />
</form>
  <p id="validemail"></p>


$(function(){
    $("#ajax_form" ).validate({
        rules: {
            email: {
                required: true,
                email: true,
                remote: {
                    url: "check_email.php",
                    type: "post",
                    data: {
                        username: function() {
                            $("#validemail").html('This email already taken');
                        }
                    }
                }
            }
        }
    });
});

Here is my check_email.php for checking the email exits or not.

mysql_connect('localhost', 'root', '');
mysql_select_db('krk');
$query = mysql_query("SELECT ID FROM customers WHERE email = '{$email}' LIMIT 1");
if (mysql_num_rows($query)) {
    return true;
}
else{
    return false;
}

The form should not submitted until another valid email he choose. how to solve this problem.

The form is submitting even the email already exit in the database. The form should not submitted until another valid email he choose. how to solve this problem. The form is submitting even the email already exit in the database. The form should not submitted until another valid email he choose. how to solve this problem. The form is submitting even the email already exit in the database.

2

There are 2 answers

5
Barmar On BEST ANSWER

The PHP script is returning the wrong values. When the query returns rows, it needs to return false because that means the email is already used. Also, it should be echoing a string, not returning a value.

mysql_connect('localhost', 'root', '');
mysql_select_db('krk');
$query = mysql_query("SELECT ID FROM customers WHERE email = '{$email}' LIMIT 1");
if (mysql_num_rows($query)) {
    echo 'false';
}
else{
    echo 'true';
}
1
DevlshOne On
<script>
var emailValid = true;
$(function(){
   $("#ajax_form" ).validate({
      rules: {
        email: {
          required: true,
          email: true,
          remote: {
            url: "check_email.php",
            type: "post",
            data: {
              username: function() {
                $("#validemail").html('This email already taken');
                emailValid = false;
              }
            }
          }
        }
      }
    });
});
</script>

Then only allow the form submit if emailValid == true;