drtanz Posted May 22, 2008 Share Posted May 22, 2008 hi i have around 40 fields all requiring the same validation, making sure that they are not empty and that they are numeric, can I check them all at once instead of writing a check for each one of them? Quote Link to comment https://forums.phpfreaks.com/topic/106793-validating-multiple-fields-at-once/ Share on other sites More sharing options...
wildteen88 Posted May 22, 2008 Share Posted May 22, 2008 Use a foreach loop foreach($_POST as $field_name => $field_value) { if(empty($_POST[$field_name]) || !is_numeric($_POST[$field_name])) { $error[$field_name] = 'error message here' } } if(isset($error) && is_array($error)) { // display error here } Quote Link to comment https://forums.phpfreaks.com/topic/106793-validating-multiple-fields-at-once/#findComment-547443 Share on other sites More sharing options...
chronister Posted May 22, 2008 Share Posted May 22, 2008 Write a function for them, or just do a really long if statement, or you could run them through a foreach loop. There really is no easy way to do it. Long forms are plain tedious. <?php function checkFields($field) { if(isset($field) && $field != '' & is_numeric($field)) { echo $field.' is not blank and is a number'; } else { $error .= $field.' did not pass validation'; } } checkFields($_POST['your_field_name']); ?> Nate wildteen88 beat me to the foreach thing... Quote Link to comment https://forums.phpfreaks.com/topic/106793-validating-multiple-fields-at-once/#findComment-547445 Share on other sites More sharing options...
GingerRobot Posted May 22, 2008 Share Posted May 22, 2008 Use a foreach loop foreach($_POST as $field_name => $field_value) { if(empty($_POST[$field_name]) || !is_numeric($_POST[$field_name])) { $error[$field_name] = 'error message here' } } if(isset($error) && is_array($error)) { // display error here } That does of course assume that you're wanting to check all your posted values. Even if that's the case, you'd need to exclude the submit button from the check: <?php foreach($_POST as $field_name => $field_value) { if((empty($_POST[$field_name]) || !is_numeric($_POST[$field_name])) && $_POST[$field_name] != 'Submit')//or whatever you called it { $error[$field_name] = 'error message here' } } if(isset($error) && is_array($error)) { // display error here } ?> If it's not all of them, then either exclude others or set an array with a list of fields you do want to check --whichever is easiest. Quote Link to comment https://forums.phpfreaks.com/topic/106793-validating-multiple-fields-at-once/#findComment-547447 Share on other sites More sharing options...
wildteen88 Posted May 22, 2008 Share Posted May 22, 2008 That does of course assume that you're wanting to check all your posted values. Even if that's the case, you'd need to exclude the submit button from the check: good point, forgot about that part. Quote Link to comment https://forums.phpfreaks.com/topic/106793-validating-multiple-fields-at-once/#findComment-547449 Share on other sites More sharing options...
drtanz Posted May 22, 2008 Author Share Posted May 22, 2008 yes i need to check all values, so will use the foreach loop excluding the submit thanks alot Quote Link to comment https://forums.phpfreaks.com/topic/106793-validating-multiple-fields-at-once/#findComment-547480 Share on other sites More sharing options...
drtanz Posted May 22, 2008 Author Share Posted May 22, 2008 ok so i cant really understand the second if statement, maybe you could explain what happens exactly there. secondly i am including this on the second page to which the first page including the form sends the data. now if there is an error i would like to redirect back to the 1st page after showing an error message, or indeed show it on the 1st page, and also populate the form with the data, how can i do that? thanks Quote Link to comment https://forums.phpfreaks.com/topic/106793-validating-multiple-fields-at-once/#findComment-547502 Share on other sites More sharing options...
chronister Posted May 22, 2008 Share Posted May 22, 2008 The second statement is to determine if there was an error message set. If $error is set and $error is an array, then display that message. I have found it easier to place the form and it's validation on one page. that way you don't have to worry about sessions to get data between the 2 to display an error message. As it stands, you will probably need to set the error messages in an array and then set that as a session to redirect them back to the page. the header(); function is what you would use to redirect. nate Quote Link to comment https://forums.phpfreaks.com/topic/106793-validating-multiple-fields-at-once/#findComment-547692 Share on other sites More sharing options...
drtanz Posted May 22, 2008 Author Share Posted May 22, 2008 How do i place them both the form and the validation on the same page? I could do a submit to self for the validation but then i would still have to submit to another page after for calculation and display of results :S Quote Link to comment https://forums.phpfreaks.com/topic/106793-validating-multiple-fields-at-once/#findComment-547706 Share on other sites More sharing options...
chronister Posted May 23, 2008 Share Posted May 23, 2008 A quick sample <?php function contact_form() // place form in a function { global $error,$state_list; set some global vars if(isset($error)){echo '<div class="error" align="center">'.$error.'</div>';};// if $error is set show it ?> <form>the form</form> <?php } // end the function if(!$_POST['contact_form']) { // call the contact form function contact_form(); } else // else, the form has been submitted so we start processing it { // process form } ?> Quote Link to comment https://forums.phpfreaks.com/topic/106793-validating-multiple-fields-at-once/#findComment-547762 Share on other sites More sharing options...
chronister Posted May 23, 2008 Share Posted May 23, 2008 I forgot to mention that the reason I have the form as a function is because in my form processing I typically will do something like this during the validation before I submit to a db or email or such <?php if(isset($error)) { contact_form(); } else { //insert to db //email the results //write to file // etc } ?> This makes it easy to determine where a forms processing is happening... but it makes for lots of code on a page.... hope it helps.. Nate Quote Link to comment https://forums.phpfreaks.com/topic/106793-validating-multiple-fields-at-once/#findComment-547784 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.