A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearHow do I check if the required fields in an html form are filled? - page 1
User InfoPosts
How do I check if the required fields in an html form are filled?#1
I've got a submission page in php with an html form that points back to the same page. I'd like to be able to check if the required fields in the form aren't filled so I can inform the user. I'd like to know how to do that with php and javascript each. However, I imagine this is a common issue so any other answers are welcome.

posted date: 2008-12-22 13:49:00


Re: How do I check if the required fields in an html form are filled?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-22 13:49:01


Re: How do I check if the required fields in an html form are filled?#3
Do the check in posting part of your php if(isset($_POST['save'])) { $fields=array(); $fields['Nimi'] = $_POST['name']; $fields['Kool'] = $_POST['school']; $fields['Aadress'] = $_POST['address']; $fields['Telefon'] = $_POST['phone']; $fields['Email'] = $_POST['email']; foreach ($fields as $key => $val) {if(trim($val)=='') {$errmsg=$key." is not filled!"; break; } } }if($errmsg == ''){ //do your saving here exit();}if(!isset($_POST['save']) || $errmsg != ''){ //show your form here // and make it to return to the same page on submit //<input name="save" type="submit" value="Save" onclick="return true;">}

posted date: 2008-12-22 13:59:00


Re: How do I check if the required fields in an html form are filled?#4
As far as JS goes you have to check before you submit. Generally this involves binding some validation function to the onsubmit event trigger of the form, and that validation function will consist of some tests for each field you're interested.Most JS libraries have validation implementations that will do most of the work for you, which sounds like it might be a good idea for you. Googling "client side validation" will yield infinite results, but this (I'm library agnostic, read and choose for yourself) should get you started*:http://blog.jquery.com/2007/07/04/about-client-side-form-validation-and-frameworks/http://tetlaw.id.au/view/blog/really-easy-field-validation-with-prototype/http://dojotoolkit.org/book/dojo-book-0-4/part-4-more-widgets/forms/validation*this is on the teaching you to fish plan

posted date: 2008-12-22 14:03:00


Re: How do I check if the required fields in an html form are filled?#5
For extra credit, once you know how to do it in PHP and JavaScript from Riho and annakata's answers, then build a way of defining a field constraint in a single form that can both be rendered as JavaScript for client-side validation and run on the server.Since you need both (client-side for user convenience, server-side because we're really very much past trusting the client at this point), it seems like quite a decent idea to support both from a single infrastructure.

posted date: 2008-12-22 14:19:00


Re: How do I check if the required fields in an html form are filled?#6
The LiveValidation library would help you out a lot:http://www.livevalidation.com/

posted date: 2008-12-28 16:36:00


Re: How do I check if the required fields in an html form are filled?#7
Is it possible to also do foreach($_POST as $k => $v)?

posted date: 2011-07-26 04:10:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.