0

I have a form and I am using jQuery validate plugin.

I am also using remote to check if the username and email are available or not but I always get false back from the script.

here is the jquery validation part:

$(document).ready(function () {
    $('#registration').validate({
        rules: {
            email: {
                email: true,
                remote:{
                    url: "./php/checkemail.php",
                    type: "POST",
                }
            },
            username: {
                minlength: 4,
                maxlength: 16,
                remote: {
                    url: "./php/checkusername.php",
                    type: "POST",
                }
            },
            messages: {
                email: {
                    email: "Please enter a valid e-mail address.",
                    remote: "This email is already registered.",
                },
                username: {
                    minlength: "Your username must be at least 4 characters long but less than 16",
                    maxlength: "Your username must be at least 4 characters long but less than 16",
                    remote: "This username is already registered.",
                },
                submitHandler: function(form) { 
                    $('#registration').ajaxSubmit();
                    return false;
                },
            });
        });

and here is the checkemail.php file:

$mysqli = mysqli_init();
if(isset($_POST['email'])) {
    $email = mysqli_real_escape_string(strtolower($_POST['email']));
    $check= mysqli_query("SELECT * FROM users WHERE LOWER email = '$email'");
    if(mysqli_stmt_num_rows($check)!=0) {
        echo json_encode(false);
    } else {
        echo json_encode(true);
    }
}

Can anyone tell me please what I did wrong and how to fix it?

Also, even if I change the script to send me true, the remote message from the validation script doesn't appear on my form.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Spluf
  • 810
  • 1
  • 11
  • 26
  • also, i have no errors in console – Spluf Feb 10 '14 at 15:41
  • You should use your web browser's developers tools to check the return data from PHP. Even if you do not use Chrome this applies to all modern web browsers (IE8 and up lol) so check here for more info http://stackoverflow.com/questions/21533285/why-the-ajax-script-is-not-running-on-iis-7-5-win-2008-r2-server#comment32640491_21533285 – MonkeyZeus Feb 10 '14 at 15:49
  • so it seams that i actually have a bunch of errors: mysqli_real_escape_string, mysqli_query, mysqli_stmt_num_rows - they all expect 2 parameters but only one is given. i checked the php documentation but if the $link part is the connection, then when i write that as the first parameter it tells me it's undefined – Spluf Feb 10 '14 at 16:22
  • I recommend either updating your current question to focus on your mysqli issue or create a new question entirely. Leave AJAX (jQuery remote validation uses AJAX) out of it until your PHP's mysqli is resolved. – MonkeyZeus Feb 10 '14 at 16:29
  • i solve it, apparently i have to wait 8 hours before i can post the answer.. but i will probably tomorrow. Thanks everyone for your help :) – Spluf Feb 10 '14 at 16:48
  • If you would like I can just post my comment as an answer for this question – MonkeyZeus Feb 10 '14 at 16:58
  • Does this answer your question? [mysqli\_query expects at least 2 parameters](https://stackoverflow.com/questions/8073278/mysqli-query-expects-at-least-2-parameters) – Dharman Jan 25 '20 at 20:03

2 Answers2

0

I think the remote function is checking for a string.

So try this:

if(mysqli_stmt_num_rows($check)!=0) {
    echo 'false';
} else {
    echo 'true';
}
koenoe
  • 110
  • 3
0

So, the problem was that the php i used was deprecated and when i changed it to the new version i did it wrong... so the correct way of doing it is this:

if(isset($_POST['email']))

{

$email = mysqli_real_escape_string($link, $_POST['email']);

$check= mysqli_query($link, "SELECT * FROM users WHERE email = '".$email."'") or die(mysqli_error($link));

if(mysqli_num_rows($check)!=0)

{

  echo 'false';

}else{

  echo 'true';

}

}

mysqli_close($link);

?>

where $link is the connection to the mysql database :

$link = mysqli_connect("$db_host", "$db_username", "$db_password", "$db_database") or die ("Error " . mysqli_error($link));

and now it works just fine.

Community
  • 1
  • 1
Spluf
  • 810
  • 1
  • 11
  • 26