php - jQuery validation form issue -
i use following code in order know if username present in database. i'm using jquery validator, , configure script in way:
$(document).ready(function(){ // validate // http://bassistance.de/jquery-plugins/jquery-plugin-validation/ // http://docs.jquery.com/plugins/validation/ // http://docs.jquery.com/plugins/validation/validate#toptions $('#profile').validate({ rules: { name: { minlength: 2, required: true }, username: { minlength: 2, required: true, remote: "../check-username.php" } }, highlight: function(element) { $(element).closest('.control-group').removeclass('success').addclass('error'); }, success: function(element) { element .text('ok!').addclass('valid') .closest('.control-group').removeclass('error').addclass('success'); } }); });
where check-username.php defined as:
<?php require_once('connection.php'); $check = "true"; $request = trim(strtolower($_request['username'])); mysql_select_db($dbname, $temp); $query_username=sprintf("select * users username = '$request'"); $resultusers = mysql_query($query_username, $temp) or die(mysql_error()); $usernamefound= mysql_num_rows($resultusers); if ($usernamefound> 0) { $check = "false"; } header("content-type: application/json; charset=windows-1251"); $result = $_request['username'].'('.$check.')'; echo $result; ?>
all works good, except recognition of existing username. maybe wrong in:
remote: "../check-username.php"
or in php file itself.
someone can me?
edit 1:
trying @mhr code obtain this:
in case "user1" exists in database. good, don't want output written under input.
http://docs.jquery.com/plugins/validation/methods/remote
the documentation has shiningly absent example of response must hints @ json:
the response evaluated json , must true valid elements, , can false, undefined or null invalid elements, using default message; or string, eg. "that name taken, try peter123 instead" display error message. more examples, take marketo demo , milk demo.
my guess should return "true" if it's valid , "false" if isn;t:
$check = true; $request = trim(strtolower($_request['username'])); mysql_select_db($dbname, $temp); $query_username=sprintf("select * users username = '$request'"); $resultusers = mysql_query($query_username, $temp) or die(mysql_error()); $usernamefound= mysql_num_rows($resultusers); if ($usernamefound> 0) { $check = false; } header("content-type: application/json; charset=windows-1251"); if($check){ $check = "true"; }else{ $check = "user: ".$request." exist, please choose user name."; } $result = "\"".$check."\""; echo $result;