han003 Posted December 9, 2012 Share Posted December 9, 2012 Hi! I've made a simple registration form, and I'm currently using javascript to validate it. The thing is, I want to be able to check if username is available before they hit the submit button, and I don't know how to integrate PHP into form validation and stuff.. Quote Link to comment Share on other sites More sharing options...
MDCode Posted December 9, 2012 Share Posted December 9, 2012 jquery will make this a lot simpler. However if a user has javascript disabled this will make it useless to use. The $.post function in jquery is very useful if you choose to continue Quote Link to comment Share on other sites More sharing options...
ChrisPHP Posted December 9, 2012 Share Posted December 9, 2012 Well if you want you can use PHP <?php $f_usr = username; // where the value of the text input of the username and password $f_pswd = password; $con=mysql_connect("localhost","root",""); if(! $con){ die('Connection Failed'.mysql_error()); } mysql_select_db("YOUR_DATABASE",$con); $result = mysql_query("SELECT * FROM ROW_IN_YOUR_DATABASE WHERE username= '" .$f_usr. "' AND password= '" .$f_pswd. "' "); $numrows=mysql_num_rows($result); if ($numrows == 0){ //If there's a row that contains this username and passowrd ENTER_WHAT_YOU_WANT_TO_DO_IN_THIS_CASE;} else{ ENTER_WHAT_YOU_WANT_TO_DO_IF_NOT;} } ?> Quote Link to comment Share on other sites More sharing options...
DavidAM Posted December 9, 2012 Share Posted December 9, 2012 Hi! I've made a simple registration form, and I'm currently using javascript to validate it. The thing is, I want to be able to check if username is available before they hit the submit button, and I don't know how to integrate PHP into form validation and stuff.. 1) Be aware that the user can disable Javascript, which means none of your validations will run. It is OK to validate using Javascript for the purpose of giving the user a quicker negative response however you absolutely must validate (again) on the server using PHP. 2) PHP runs on the server. It is done and has gone away before the browser ever gets the page to display. So, in fact, you cannot "integrate PHP into form validation" (on the client side). In order to "integrate" them, the browser makes a call to the server which will run a PHP script and send a result. This is usually handled using AJAX (Asynchoronous Javascript and XML). Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 9, 2012 Share Posted December 9, 2012 1) Be aware that the user can disable Javascript, which means none of your validations will run. It is OK to validate using Javascript for the purpose of giving the user a quicker negative response however you absolutely must validate (again) on the server using PHP. I'll add a clarification to DavidAM's spot-on statements. Even when you are using AJAX, the validation for something such as verifying if a username is taken still takes place "on the server", and it is important to revalidate when the form is posted. The username could have been taken between the time the validation was done using AJAX and the time the user ultimately submitted the form. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.