JQuery check mysql database if username is already executed

I know this is possible, but I am confused about how this works. I searched online for how this is done and I think I missed something in the process

I have this jQuery code:

$("#Contact_Info").validate({  
        rules: {
            username: {
                required: true,
                minlength: 6,
                remote: "check-username.php"
                }
            },
        messages: {
            username:{
                remote: "This username is already taken! Try another."
            }
        }
  });

How can I write my php page to talk with jQuery code?

So far, I:

$name = mysql_real_escape_string($name);

$query = "SELECT USERNAME FROM T_MEMBER WHERE USERNAME='$name';";
$res = mysql_query($query);
if (mysql_num_rows($res) > 0) {
   return false;
} else
   return true; 

How to get $ name variable from javascript? Should I set "remote" as POST?

Thanks in advance, Ian McCullough

+5
source share
2 answers

Your PHP page only communicates with Javascript through data output. Depending on the plugin, you can achieve true/ falseby repeating 1 or 0, or perhaps even a line trueorfalse

:

JSON true false, undefined or null

, PHP :

if (mysql_num_rows($res) > 0) {
    $output = true;
} else {
    $output = false;
}
echo json_encode($output);
+4

All Articles