Problem with check username and email available with ajax, php and jquery Hi .__. thanks for that amazing piece of code, and the step by step tutorial. I used first for check the user name, but I also need it for the e-mail. I had to make an user_availability2 and change the id=”msgbox” to id=”msgbox2″ (also in the function) and it was working great (when an email or user name were already in the db, I disabled the send button) until I noticed that if one of those were available the button turn to able again. So I have two options make only one function and send both of the variables or make a codition to check if one of them isn’t available and disable the button.
I’m a beginner :$ so I know a little about logic and I have the idea, but i turns complicate when i have to translate it to code. Any idea is welcome =) thanks
//This is the first function $(document).ready(function() { $(”#user”).blur(function() { //remove all the class add the messagebox classes and start fading $(”#msgbox”).removeClass().addClass(’messagebox’).text(’Checking…’).fadeIn(”slow”); //check the username exists or not from ajax $.post(”user_availability.php”,{ user:$(this).val() } ,function(data) { if(data==’no’) //if username not avaiable { $(”#msgbox”).fadeTo(200,0.1,function() //start fading the messagebox { //add message and change the class of the box and start fading $(this).html(’This User name Already exists’).addClass(’messageboxerror’).fadeTo(900,1);
}); document.form1.button.disabled=true;
} else { $(”#msgbox”).fadeTo(200,0.1,function() //start fading the messagebox { //add message and change the class of the box and start fading $(this).html(’Username available to register’).addClass(’messageboxok’).fadeTo(900,1); }); document.form1.button.disabled=false; }
});
}); });
//The second one for validate email $(document).ready(function() { $(”#email”).blur(function() { //remove all the class add the messagebox classes and start fading $(”#msgbox2″).removeClass().addClass(’messagebox’).text(’Checking…’).fadeIn(”slow”); //check the username exists or not from ajax $.post(”user_availability2.php”,{ email:$(this).val() } ,function(data) { if(data==’no’) //if username not avaiable { $(”#msgbox2″).fadeTo(200,0.1,function() //start fading the messagebox { //add message and change the class of the box and start fading $(this).html(’This E-mail Already exists’).addClass(’messageboxerror’).fadeTo(900,1);
}); document.form1.button.disabled=true;
} else { $(”#msgbox2″).fadeTo(200,0.1,function() //start fading the messagebox { //add message and change the class of the box and start fading $(this).html(’E-mail available to register’).addClass(’messageboxok’).fadeTo(900,1); }); document.form1.button.disabled=false; }
});
}); });
//PHP code //This is user_availability.php //this varible contains the array of existing users connect(); $query = “SELECT * FROM users WHERE user = ‘”.$_POST['user'].”‘”; $result = mysql_query($query);
//checking weather user exists or not in $existing_users array if (mysql_num_rows($result)>0) { //user name is not availble echo “no”; } else { //user name is available echo “yes”; } disconnect();
//This is user_availability2.php //this varible contains the array of existing users connect(); $query = “SELECT * FROM users WHERE email = ‘”.$_POST['email'].”‘”; $result = mysql_query($query);
//checking weather user exists or not in $existing_users array if (mysql_num_rows($result)>0) { //user name is not availble echo “no”; } else { //user name is available echo “yes”; } disconnect();
Started By lauretta.gs on Sep 16, 2009 at 9:42:48 AM This message has been edited.