0

I have a problem with using jQuery validate remote function with php. I try to check the user id with query whether the user id is exist or not. But it alway show user is in used.

Here is my jQuery validate code:

userID: {
                    required: true,
                    minlength: 5,
                    remote : {
                        url: "checkUserId.php",
                        type: "post"
                    }
                },

Here is my php checking code:

<?php
     include "session.php";
     include 'dbConnect.php';

     global $conn;

     $requestedID  = $_POST['userID'];

     $query = "SELECT * FROM Users WHERE _userID = '$requestedID'";
     $result = sqlsrv_query($conn,$query);

     $row_count = sqlsrv_num_rows($result);
     if($row_count === false){
         echo 'false';
     }
     else if($row_count >=0){
         echo 'true';
     }
?>

Anything I did wrong in my query?

9
  • Have you tried debugging $_POST to ensure that the correct value is being passed through by jQuery? Commented Apr 28, 2015 at 1:59
  • 1
    The column is _userID or userid? Also passing user provided data directly to the sql opens you to injections. Commented Apr 28, 2015 at 2:00
  • @scrowler Yes already debug before Commented Apr 28, 2015 at 2:02
  • @chris85 Its okay, ignore the injections first. Please stick to the question please... Commented Apr 28, 2015 at 2:02
  • the table column is _userID Commented Apr 28, 2015 at 2:03

2 Answers 2

2

Change your if else condition.

if($row_count == 0){
echo 'true';
}else{
echo 'false';
}

true = not exist, false = exist

Sign up to request clarification or add additional context in comments.

5 Comments

did you tried to echo $row_count? what's the result?
how to echo the result as i using remote... it does not show anything on my echo
yeah right, but you can access directly through the browser your php file(checkUserId.php) to see the result.
this is what i get Notice: Undefined index: userID in C:\xampp\htdocs\TNBCMS(Develop)\checkUserId.php on line 7 false
it means that you are not getting the value of this $_POST['userID'] . check your form if it has a method post and also echo $_POST['userID'] .
0

I had successfully solved it with this code. Thanks for all the support.

Here is my updated code:

<?php
     include "session.php";
     include 'dbConnect.php';

     global $conn;

     $requestedID  = $_REQUEST['userID'];

     $query = "SELECT * FROM Users WHERE _userID = '$requestedID'";
     $params = array();
     $options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
     $result = sqlsrv_query($conn,$query , $params, $options);


    $row_count = sqlsrv_num_rows($result);
    if($row_count >= 1){
        echo 'false';
    }
    else{
        echo 'true';
    }


?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.