How To (Effectively) use jQuery to Match the Value of an Input with a Database?
I've been working on a PHP project of mine, and I am attempting to add a feature that tells the user whether or not the username they entered in a register form is already taken as they update the input. When starting this, I thought I knew how to code this- but I ran into some problems along the way.
When I post the data to my PHP script, I get the 'all clear'. I've double checked it, as well as FireBugged the post- all variables are present, and the server returns exactly as expected. I've inserted a test field into my database so I can see if it works. The name in the DB is 'Cameron'.
Now, for my code.
function checkUsername() {
$.post(rootpath + "/inc/php/handlers/checkusername.php", {
submit: "submitted",
开发者_高级运维 username: $('#username').val()
}, function (data) {
if (data = 'not_taken') {
$('#cui').html('<img src="' + rootpath + '/inc/images/icons/Checkmark-32.png" />');
}
else {
$('#cui').html('<img src="' + rootpath + '/inc/images/icons/block-icon-32.png" width="32" height="32" />');
}
}, "html");
}
and then
<input type='text' name='username' id='username' onkeyup='checkUsername();' />
In this case, I was hoping to change the HTML of a div to that of an image. But what the code does is switch to the checkmark image (the first one), regardless of the return (see below).
I believed this was a problem with the method I used for updating the div, .html()
. But I then discovered that, even if the first combination submitted was that of the database (returning 'taken'), the output would still be the check-mark image.
I need the image to update with, obviously, the correct one. I'm pretty sure it's not a PHP problem as the script I set up returns the data expected.
Thanks in advance for any help!
You're using the assignment operator instead of checking for equality in your conditional:
if(data = 'not_taken') {
should be
if(data == 'not_taken') {
function doAjaxPost(username)
{
var username= $('#username').val();
$.ajax({
type: "POST",
url: "/inc/php/handlers/checkusername.php",
data: "username="+username+,
success: function(resp){
//alert($Msg);
},
error: function(e){
//alert($Msg);
}
});
}
in checkusername.php
$SelectQuery=" select usename from userswhere usename='".$_POST['usename']."' ";
if($Row=$DB->Select($SelectQuery,'select_capacity',$Module))
{
$select_record=1;
}
$sbval=mysql_num_rows($Row);
if($sbval>=1)
return "alreay exist";
It will be done in this way . show finel returned message in response div.
It's a way to do. Please change the code according to ur CMS ,architecture,framework.
I hope it will help u. cheers :)
enter code here
精彩评论