archer9134 Posted January 5, 2012 Share Posted January 5, 2012 Hi, i am still trying to learn html and php, but i am also trying to create a website which contains a registration form. I want the validation of each field of that form to be performed automatically. So using onBlur, whenever the user steps out of a textbox field, i want to execute a php function located in another file, which will check the value of the field and display a note if the value is invalid. I want to do this using as less javascript as possible. I am not sure if what i am asking is possible but if it is i would like to know the easiest way to do this. I would appreciate any help i can get. Thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/254414-automatic-validation-of-form-with-php/ Share on other sites More sharing options...
Nodral Posted January 5, 2012 Share Posted January 5, 2012 You can do this, but as PHP acts on the server and not the browser, you would need to return the form to the server to check the validation after every onBlur event. Javascript is the best to use for form validation prior to sending to the server for a final more thorough check. Quote Link to comment https://forums.phpfreaks.com/topic/254414-automatic-validation-of-form-with-php/#findComment-1304488 Share on other sites More sharing options...
archer9134 Posted January 5, 2012 Author Share Posted January 5, 2012 yes but i want some checks to be done by php, like checking if the username exists in the database or validating the email. Maybe i could check only these two fields with form submission to php and the rest with javascript. But still i don't know how i can call a specific php function using onBlur. Quote Link to comment https://forums.phpfreaks.com/topic/254414-automatic-validation-of-form-with-php/#findComment-1304490 Share on other sites More sharing options...
Nodral Posted January 5, 2012 Share Posted January 5, 2012 What you do is use Javascript to check the completed fields on the form with basic validation using onBlur (completed/incomplete, numbers, email format etc). Once these criteria are all satisfied, you then pass all the form values back to the server to check against your DB using PHP. Javascript / client side should not be used for referencing against a DB, just for checking the basics as to whether the form fields are filled in correctly. You need to write some code to connect to your DB in PHP and check the values from the form (sent back using POST method) and then produce a success / fail output to be sent back to the browser. There are loads of good tutorials around this, just use google to find them. Once again, use Javascript to validate the FORMAT of the input, and use PHP to validate against your DB. A hint for the future though, make sure you "clean" the user inputs prior to doing any sort of DB work to prevent SQL injection etc Quote Link to comment https://forums.phpfreaks.com/topic/254414-automatic-validation-of-form-with-php/#findComment-1304494 Share on other sites More sharing options...
archer9134 Posted January 5, 2012 Author Share Posted January 5, 2012 ok, i'll do that then. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/254414-automatic-validation-of-form-with-php/#findComment-1304496 Share on other sites More sharing options...
Philip Posted January 5, 2012 Share Posted January 5, 2012 You can look into using AJAX for checking these. It will allow you to have JS call a PHP script (which can then check for a specific username/email/etc in the database.) Using a JS framework such as jQuery or prototype will help you dive into the JS world easily Quote Link to comment https://forums.phpfreaks.com/topic/254414-automatic-validation-of-form-with-php/#findComment-1304530 Share on other sites More sharing options...
archer9134 Posted January 5, 2012 Author Share Posted January 5, 2012 this is the second time someone suggests jquery so i guess i will have to look into it. thanks Quote Link to comment https://forums.phpfreaks.com/topic/254414-automatic-validation-of-form-with-php/#findComment-1304594 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.