JQuery Test using remote method to check if username exists

I want to check if a username exists in the database using jQuery.validate, so here is what I still have:

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 accepted, even if it is not.

I am using jQuery Mobile 1.9.1

Thanks in advance.

+7
source share
2 answers

I managed to get this to work by changing the PHP method I used, here is 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; ?> 

Thank you all for your help :)

+6
source

I have two resources to view.

The official example from the validate module: http://jquery.bassistance.de/validate/demo/milk/

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

A possible solution is that the answer, in my opinion, should not be json-encoded. Since json needs key value pairs, sending only the value will not work. So try just repeating this as the lines "true" or "false".

Secondly, validation uses GET for the form submission method, not POST.

NOTE: ONLY A POSSIBLE JQuery SOLUTION IS FOUND

+2
source

Source: https://habr.com/ru/post/945110/


All Articles