Excluding form fields if values are empty

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KHurst
    New Member
    • Apr 2008
    • 4

    Excluding form fields if values are empty

    I have created a form that I email the submitted results to an email address. The form has several optional fields that I would like to exclude from the sent email if they are empty. Below is the PHP code that I am using now.

    foreach ($_POST as $field=>$value)
    $message .= "$field: $value\n";
    mail($to, $subject, $message);
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Try this:
    [php]
    if(!strlen(trim ($_value)) < 1)
    {
    # do mail
    }
    [/php]
    Regards :)

    Comment

    • KHurst
      New Member
      • Apr 2008
      • 4

      #3
      Just to help the next person. This is the code that worked for me. Thanks!


      foreach ($_POST as $field=>$value)
      if(!strlen(trim ($vlaue)) < 1) {
      $message .= "$field: $value\n";
      }
      mail($to, $subject, $message);

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by KHurst
        Just to help the next person. This is the code that worked for me. Thanks!


        foreach ($_POST as $field=>$value)
        if(!strlen(trim ($vlaue)) < 1) {
        $message .= "$field: $value\n";
        }
        mail($to, $subject, $message);
        Sweet!

        Remember to use code tags when posting code, though.

        See you around dude!

        Comment

        Working...