0

I have just started studying programming and I've been stuck for hours in this problem. I have a sign up form which asks for email and it should check the mysql database if the email is already registered. I am currently trying to use the remote method from the Jquery validator plugin but I can't seem to make it work.

emailcheck.php

<?php
include_once 'dbconnect.php';
$email = $_POST['CusEadd'];
$query = "SELECT CusEadd FROM customer WHERE CusEadd='$email'";
$result = mysql_query($query);
$count = mysql_num_rows($result);

if($count>0){
    echo json_encode(FALSE);
}
else {
    echo json_encode(TRUE);
}

?>

form-validation.js

CusEadd: {  
            required:true,
            email:true,
            remote:"emailcheck.php"
        }
3
  • If you're using the jQuery validate plugin, I believe it makes a GET request by default, you'd have to specify POST if that's what you want Commented Oct 25, 2016 at 19:31
  • 1
    Especially if you're just starting to learn programming please don't use PHP's mysql_* functions. They were deprecated in PHP 5.5, which is so old that it no longer receives security updates, and completely removed in PHP 7. Instead, use PDO or mysqli_*. Commented Oct 25, 2016 at 19:34
  • 1
    Yeah. I would definitely stop using mysql and learn how to use PDO. thanks guys! Commented Oct 25, 2016 at 19:44

1 Answer 1

1

The documentation for jQuery Validate states that

The serverside resource is called via jQuery.ajax (XMLHttpRequest) and gets a key/value pair corresponding to the name of the validated element and its value as a GET parameter.

The serverside response must be a JSON string that must be "true" for valid elements, and can be "false", undefined, or null for invalid elements

You're checking for $_POST['CusEadd'], and assuming you have an element with that name, you still have to specify POST as a method

CusEadd: {  
    required:true,
    email:true,
    remote: {
        url  : "emailcheck.php",
        type : "POST"
    }
}

Also, you want to return the string "true", not PHP booleans, which usually converts to numbers

if($count>0){
    echo json_encode("false");
}
else {
    echo json_encode("true");
}

And do not use mysql_* in production code, your code is open to SQL attacks by simply passing something like 'OR 1=1 to the POST variable

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

1 Comment

Thank you!! Thank you very much! You are a real life saver. And also I'm gonna remove the mysql and use mysqli instead. thank you again!!

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.