Jump to content

[SOLVED] List Fields Only If They Have A Value


dwayneparton

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.