Take the 2-minute tour ×

Currently I am making an online enquiry form with a set of fields that are non-mandatory.

If a non-mandatory form field is not filled-in out, I would like to make it so that it doesn't come through in the processed email.

For instance; if someone does not enter their telephone number, the "Telephone: $atelephone" component does not come through.

if ($atelephone != '') { 
echo "Telephone: ".$atelephone;  

I figure the code should have something like above put in it, though I am struggling to connect the dots. Any help would be greatly appreciated. (I hope this makes sense).


// Base form items

$asender = $HTTP_POST_VARS['name'] ." <". $HTTP_POST_VARS['email'] .">";
$asubject = "Email Enquiry: ".$HTTP_POST_VARS['subject'];
$arecipient = "recipient@websiteaddress.com.au";

// Mail form variables //

$aname = $HTTP_POST_VARS['name'];
$aemail = $HTTP_POST_VARS['email'];
$atelephone = $HTTP_POST_VARS['telephone'];
$asuburb = $HTTP_POST_VARS['suburb'];
$aenquiry = $HTTP_POST_VARS['enquiry'];

Please note: this is an email 
generated from the Website.

Name: $aname
Email: $aemail
Telephone: $atelephone
Suburb: $asuburb


================================ ","FROM:$asender"); 

header('Location: /thank-you.php');

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You're on the right track. The last step is make a string to input in your final message:

$_POST['telephone'] ?  
  $telephoneString = "Telephone: ".$_POST['telephone'] ."\n" : 
  $telephoneString = "";

(The \n at the end of the string makes a newline.)

Then, output the string in the message. It will be empty, or not.

  "foo bar baz


This may work better for individual form fields. However, for elegance, I prefer the solution from @mazzzzzz.

share|improve this answer
I agree I think @mazzzzzz solution is really neat. Thank you very much for your solution. Unfortunately I think I am going to have to generate the individual form fields. –  Brandrally Jun 28 '11 at 6:06
@Brandrally - one thing you may want to check is security in your code. It's generally a bad idea to send raw post data anywhere without filtering it first. What to filter is a whole new question. –  Steve Jun 28 '11 at 6:08
a very good point –  Ben Jun 28 '11 at 7:38

Hm, loop through the POST array, if the field is empty, don't add it..

Something like:

$acceptedInputs = array('name', 'email', etc.);
$spacesBA = array('message'=>array(1,2)); //Spaces before/after, first is before, second is after.  Default is none.

$emailBits = array();

foreach ($_POST as $name=>$value)
    if (!in_array($name, $acceptedInputs)) //Don't want them to submit unknown fields
    if (!empty($value))
        $emailBits[] = 
str_repeat("\n",(isset($spacesBA[$name][0])?$spacesBA[$name][0]:0) /* Add before lines */
 . $name . ' : ' . $value . 
str_repeat("\n",(isset($spacesBA[$name][1])?$spacesBA[$name][1]:0)); /*Add after lines */
$emailBody = "
Please note: this is an email 
generated from the Website.
$emailBody .= implode("\n",$emailBits);
$emailBody .= "

share|improve this answer
Thanks @mazzzzz The above is a really good solution, though the enquiry form in question has over 200 fields, and so it needs to be styled a little specifically. –  Brandrally Jun 28 '11 at 6:01
Accidentally pressed return. Just so I can cluster answers together beneath a heading, and place additional returns in where appropriate to make the email a little easier to read. –  Brandrally Jun 28 '11 at 6:03
I guess you could make another array, of how many returns per bit, then add that many on each loop. Tbh, with a form that side, this is the only practical solution –  Ben Jun 28 '11 at 6:58
Edited it include a formatting system (if you want it) –  Ben Jun 28 '11 at 7:03

Your Answer


