0

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
jvk
  • 2,133
  • 3
  • 19
  • 28
  • You must `echo`, not `return`, `"true"` or `"false"` using `json_encode()`. Also, the `data` option is for sending extra data to the server, NOT for setting an error message. See the examples in the docs and the linked duplicate. – Sparky Jan 06 '17 at 19:48

2 Answers2

1

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';
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks for your help. I am little curious how it is working. can you explain me please – jvk Jan 01 '17 at 15:15
  • It's like any other AJAX or PHP script -- the client only gets what you echo. – Barmar Jan 01 '17 at 15:21
  • Although `"true"` works, technically, you should only be echoing a JSON encoded string.... `echo json_encode("true")`, etc. You can also echo any JSON encoded string and it will trigger a validation error and the string becomes the validation error message. – Sparky Jan 06 '17 at 19:51
  • @Sparky, can we use jquery validation onblur instead of onkey for remote validation – jvk Mar 15 '19 at 16:30
  • @KrishnaJonnalagadda, you cannot alter `onfocusout` or `onkeyup` for only one rule. If you customized these, it would apply to all rules. Search SO for possible workarounds. – Sparky Mar 15 '19 at 17:04
0
<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;

DevlshOne
  • 8,357
  • 1
  • 29
  • 37
  • The `data` option is for sending extra data to the server, NOT for setting an error message or anything else. See the examples in the docs. – Sparky Jan 06 '17 at 19:49