i have a php code to check if a user already exists in my db , but i would like it to go through jquery first .
This way instead of getting an error message on a new page , the user simply gets a message that the user already exists and can change the name.
Looks like this :
PHP CODE:
<?php
$link = mysqli_connect("localhost","root","","data1") or die("Error " . mysqli_error($link));
$usercheck = "csdcsdsdf";
$sanitizeduser= filter_var($usercheck, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
$result = $link->query("SELECT username FROM users WHERE username = '".$sanitizeduser."' ");
$row_cnt = $result->num_rows;
if($row_cnt>0){
echo "User Exists";
}else{
echo "No User found";
}
?>
I'm using a predefined jquery code (this script : http://jqueryvalidation.org/)'
part of my jquery code to validate names :
names: function(value, element) {
return this.optional(element) || /^\w+$/.test(value);
},
Full Jquery code :
file 1 : http://jsfiddle.net/EjSbd/1/ (script.js)
file 2 : http://jsfiddle.net/qM4Uz/1/ (jquery.validate.js)
One of the easier ways is to use one of jQuery's Ajax functions, .load()
So first you make an HTML div where you want your dynamically generated content to be:
Next you make a Javascript function that's just for changing the div:
Then anywhere in the Javascript where you want it, like right after your validation, just call:
Try this, You need to add
remote: "usernamecheck.php"
Assumed your php file name isusernamecheck.php
Ref: http://jqueryvalidation.org/category/methods/
If you are using jQuery validation plugin then here is the solution. This is active code. you need to use remote: . This is my existing code. This will help you to get idea.
Here is complete guideline. Following this link. They have example code. http://imamiscool.wordpress.com/2009/06/29/check-email-availability-using-jquery%E2%80%99s-ajax-part-2-easy-way/