dwayneparton Posted August 28, 2009 Share Posted August 28, 2009 I am new to the forums, and was hoping for some help. I have a php script that prints all the fields and values of an e-form when submitted. Here is what I basically have. foreach($_POST as $field => $val) { $message = $message . trim($field)." = ". trim($val). "\n" ; } What I want is for the script to only list the fields if they have a value. If someone, for instance, leaves one of the input boxes empty then it doesn't show up in the email unless it has a default value. Hope that makes sense. Please Help. Link to comment https://forums.phpfreaks.com/topic/172330-solved-list-fields-only-if-they-have-a-value/ Share on other sites More sharing options...
ignace Posted August 28, 2009 Share Posted August 28, 2009 print !empty($val) ? $message . trim($field)." = ". trim($val). "\n" : ''; Link to comment https://forums.phpfreaks.com/topic/172330-solved-list-fields-only-if-they-have-a-value/#findComment-908612 Share on other sites More sharing options...
dwayneparton Posted August 28, 2009 Author Share Posted August 28, 2009 Thanks for the reply. I thought I would be able to figure out how to use the answer with my code, but I can not. Here is what all of my code looks like. foreach($_POST as $field => $val) { if($i%2 == 0){$message = $message.'<tr bgcolor="#E9EDF5"><td valign="top"><b>'.trim($field).'</b></td><td>'.trim($val).'</td></tr>' ; $i++; } else{$message = $message.'<tr><td valign="top"><b>'.trim($field).'</b></td><td>'.trim($val).'</td></tr>' ; $i++;} } My boss wanted the emails to come out in pretty HTML. Sorry for the lack of detail the first time. Thanks for the Help. Link to comment https://forums.phpfreaks.com/topic/172330-solved-list-fields-only-if-they-have-a-value/#findComment-908619 Share on other sites More sharing options...
lemmin Posted August 28, 2009 Share Posted August 28, 2009 You don't have to do it as an inline if. You can just do it like this: foreach($_POST as $field => $val) { if (!empty($val)) { if($i%2 == 0){$message = $message.'<tr bgcolor="#E9EDF5"><td valign="top"><b>'.trim($field).'</b></td><td>'.trim($val).'</td></tr>' ; $i++; } else{$message = $message.'<tr><td valign="top"><b>'.trim($field).'</b></td><td>'.trim($val).'</td></tr>' ; $i++;} } } Link to comment https://forums.phpfreaks.com/topic/172330-solved-list-fields-only-if-they-have-a-value/#findComment-908621 Share on other sites More sharing options...
dwayneparton Posted August 28, 2009 Author Share Posted August 28, 2009 Worked perfectly! THANKS! Link to comment https://forums.phpfreaks.com/topic/172330-solved-list-fields-only-if-they-have-a-value/#findComment-908628 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.