Ken2k7 Posted August 21, 2007 Share Posted August 21, 2007 Say I have a form, a really long form, is there a way to check if all the fields in the form are empty without saying a million if statements? Quote Link to comment https://forums.phpfreaks.com/topic/65987-solved-empty-form/ Share on other sites More sharing options...
trq Posted August 21, 2007 Share Posted August 21, 2007 <?php $empty = true; foreach($_POST as $v) { if (!empty($v)) { $empty = false; break; } } if ($empty) { echo "no fields where filled in"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65987-solved-empty-form/#findComment-329940 Share on other sites More sharing options...
Ken2k7 Posted August 21, 2007 Author Share Posted August 21, 2007 OH...I see. Thanks so much. Say, is there a way to make that a function so I don't have to type that over and over everytime? Quote Link to comment https://forums.phpfreaks.com/topic/65987-solved-empty-form/#findComment-329978 Share on other sites More sharing options...
trq Posted August 21, 2007 Share Posted August 21, 2007 Of course. read about functions here. Quote Link to comment https://forums.phpfreaks.com/topic/65987-solved-empty-form/#findComment-329981 Share on other sites More sharing options...
Ken2k7 Posted August 21, 2007 Author Share Posted August 21, 2007 Oh no, I know how to make a function, I just don't know what parameter I would give this one, if any that is. Quote Link to comment https://forums.phpfreaks.com/topic/65987-solved-empty-form/#findComment-329989 Share on other sites More sharing options...
LiamProductions Posted August 21, 2007 Share Posted August 21, 2007 Is'nt it more safe to write out all these if functions? Quote Link to comment https://forums.phpfreaks.com/topic/65987-solved-empty-form/#findComment-329990 Share on other sites More sharing options...
Ken2k7 Posted August 21, 2007 Author Share Posted August 21, 2007 Is'nt it more safe to write out all these if functions? Define "more safe." Quote Link to comment https://forums.phpfreaks.com/topic/65987-solved-empty-form/#findComment-329993 Share on other sites More sharing options...
Ken2k7 Posted August 21, 2007 Author Share Posted August 21, 2007 *bump* Quote Link to comment https://forums.phpfreaks.com/topic/65987-solved-empty-form/#findComment-330079 Share on other sites More sharing options...
trq Posted August 21, 2007 Share Posted August 21, 2007 I think what Liam might be getting at is the usefullness of said function. it just doesn't seem very functional to check if all fields are empty, you usually need to check fields individually. The code I wrote would return false even if only one field is filled in. Anyway... here it is a function. <?php function all_empty_post() { $empty = true; foreach($_POST as $v) { if (!empty($v)) { $empty = false; break; } } return $empty; } if (all_empty_post()) { echo "no fields where filled in"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65987-solved-empty-form/#findComment-330082 Share on other sites More sharing options...
Ken2k7 Posted August 21, 2007 Author Share Posted August 21, 2007 Oh, I see. But it's ridiculous to make a lot of if statements. Would this work on inserting values if the field is not empty? I'm not sure if php can use void functions since this one, if it works, doesn't return anything. <?php function all_empty_post() { $empty = true; foreach ($_POST as $v) if (!empty($v)) { mysql_query("INSERT INTO tableName ($v) VALUES ($_POST[$v])") or die(mysql_error()); $empty = false; } return $empty; } if (all_empty_post()) { echo "no fields where filled in"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65987-solved-empty-form/#findComment-330130 Share on other sites More sharing options...
LiamProductions Posted August 21, 2007 Share Posted August 21, 2007 If you was to use Ifs and else's you would be able to choose what you want to do like make a maximum of 8 chracters or minimum of 3 and you can check if fields match ect. Quote Link to comment https://forums.phpfreaks.com/topic/65987-solved-empty-form/#findComment-330148 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.