1

I am using the JQuery Validation plugin which is working great apart from the remote validation on a username to be unique.

The script is returning "true" or "false" which you will see from the below code. But I am not sure why it isn't displaying an error... It's worth mentioning the other errors on User are displayed.

Jquery Validation Rules:

User: {
                required: true,
                minlength: 6,
                remote:{
                    url: 'scripts/userCheck.php',
                    type: "post"
                }
            }

JQuery Validation Messages:

User: {
                    required: "Please Enter a Username",
                    minlength: "Username must be more than 6 characters in Length",
                    remote: "User already exists"
                }

userCheck.php:

<?php
/**
 * Created by PhpStorm.
 * User: nathanenglish5
 * Date: 30/12/2015
 * Time: 09:05
 */
include_once "init.php";
include_once "../resources/signup.class.php";
$signup = new signup($DB_con);

$return = "false";
$count = 0;

if (isset($_Post['User'])) {
    $uid = $_Post['User'];
    $sql = "SELECT COUNT(username) FROM username WHERE username = '$uid'";
    $count = $signup->dataview($sql);

    if($signup->dataview($sql)==0){
        $return = "true";
    }
}
?>

All the dataview class does is return a number.

Any help or guidence would be great!

Nathan English
  • 684
  • 2
  • 10
  • 28

2 Answers2

0

Try this:

//Our validation script will go here.

    $(document).ready(function(){

        //validation implementation will go here.
        $("#form_id").validate({
            rules: {

                user: {
                    required: true,
                   minlength: 6,
                },
               remote: {
                  url: 'scripts/userCheck.php',
                type: "post"
               }
           },
           messages: {
               user: {
                    required: "Please Enter a Username",
                minlength: "Username must be more than 6 characters in Length",

               },
               remote: {
                   remote: "User already exists"

               }
           }
       });
 })
Manoj S Kadlag
  • 250
  • 2
  • 13
0

I Read a few other forums and found that I needed to echo the result and changed the userCheck.php to the below:

<?php
/**
 * Created by PhpStorm.
 * User: nathanenglish5
 * Date: 30/12/2015
 * Time: 09:05
 */
include_once "init.php";
include_once "../resources/signup.class.php";
$signup = new signup($DB_con);

$return = "false";
$count = 0;

if (isset($_POST['User'])) {
    $uid = $_POST['User'];
    $sql = "SELECT COUNT(username) FROM username WHERE username = '$uid'";
    $count = $signup->dataview($sql);

    if($signup->dataview($sql)==0){
        $return = "true";
    }
}
echo $return;
?>
Nathan English
  • 684
  • 2
  • 10
  • 28