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.

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.

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++;}
      }
   }

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.