开发者

PHP: Do not email form fields that are left blank

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 ap开发者_JAVA技巧preciated. (I hope this makes sense).

<?php 

// 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'];

mail("$arecipient","$asubject", 
"
===========================================
Please note: this is an email 
generated from the Website.
=========================================== 

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

Message:
$aenquiry 

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

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

?>


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
  ===========================================".
  $nameString.
  $emailString.
  $telephoneString.
  $suburbString;

Edit

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


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
        continue;
    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 .= "

================================ ";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜