7

I want to validate if username exists in database using jQuery.validate so here's what I have so far:

jQuery:

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

check-username.php:

<?php

require_once "./source/includes/data.php";
header('Content-type: application/json');

$name = mysql_real_escape_string($_POST['username']);

$check_for_username = mysql_query("SELECT username FROM mmh_user_info WHERE username='$name'");

if (mysql_num_rows($check_for_username) > 0) {
    $output = true;
} else {
    $output = false;
}
echo json_encode($output);
?>

This code always shows an error that the username is taken even if it's not.

I'm using jQuery Mobile 1.9.1

Thanks in advance.

Rounds
  • 1,879
  • 4
  • 19
  • 30
  • 2
    Can you explain the question a little bit more? Which part seems to work, and what doesn't? – CP510 May 16 '13 at 00:27
  • On the registration page, it does not check if the user is taken. – Rounds May 16 '13 at 00:28
  • Open the network tab. Is a request sent to the page "check-username.php" on submit? if so, what is the response code (200,404,500 etc.) and what is the response content? – Matanya May 16 '13 at 01:28
  • Okay, I'm sorry about this, there was no requests sent to check-username.php the file was in the wrong place. I fixed it but now it always show that the username is taken even if it's not! – Rounds May 16 '13 at 01:47

2 Answers2

8

I've managed to get this to work by changing the PHP technique I was using, here's my PHP:

<?php
require_once "./source/includes/data.php";
header('Content-type: application/json');
$request = $_REQUEST['username'];

$query = mysql_query("SELECT * FROM mmh_user_info WHERE username ='$username'");
$result = mysql_num_rows($query);
if ($result == 0){
$valid = 'true';}
else{
$valid = 'false';
}
echo $valid;
?>

Thanks everyone here for your help :)

Rounds
  • 1,879
  • 4
  • 19
  • 30
2

I have two resources for to look at.

Official example from the validate plugin: http://jquery.bassistance.de/validate/demo/milk/

jQuery forum solution: http://forum.jquery.com/topic/jquery-validation-plugin-remote-validation-problem-locks-up

The possible solution is that the response does not need to be json encoded as far as I can tell. Since json needs key value pairs, suppling just the value won't work. So try to just echo it out as 'true' or 'false' strings.

Second, the validate uses GET for the form submission method, not POST.

NOTE: JUST FOUND POSSIBLE SOLUTION QUESTION jQuery Remote validation

Community
  • 1
  • 1
CP510
  • 2,297
  • 15
  • 15
  • Well that does a bit actually. Unfortunatly I am not to knowledgable on jQuery Mobile. If anyone else is, they should feel free to correct us. :) – CP510 May 16 '13 at 01:19