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. Quote Link to comment 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" : ''; Quote Link to comment 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. Quote Link to comment 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++;} } } Quote Link to comment Share on other sites More sharing options...
dwayneparton Posted August 28, 2009 Author Share Posted August 28, 2009 Worked perfectly! THANKS! Quote Link to comment 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.