PHP:不要将空白的表单字段邮寄 [英] PHP: Do not email form fields that are left blank
问题描述
目前,我正在制作一个非强制性字段的在线查询表单。
如果一个非强制性表单字段未填写,我想让它在处理的电子邮件中不会出现。 p>
例如;如果有人没有输入他们的电话号码,电话:$ atelephone组件不会通过。
if($ atelephone!=''){
echo电话:$ atelephone;
}
我认为代码应该有上面的东西,虽然我努力连接点。任何帮助将不胜感激。 (我希望这是有道理的)。
<?php
//基本表单项
$ asender = $ HTTP_POST_VARS ['name']。<。 $ HTTP_POST_VARS ['email']。>;
$ asubject =电子邮件查询:$ HTTP_POST_VARS ['subject'];
$ arecipient =recipient@websiteaddress.com.au;
/ ******************************************* ************** /
//邮件表单变量//
$ aname = $ HTTP_POST_VARS ['name'];
$ aemail = $ HTTP_POST_VARS ['email'];
$ atelephone = $ HTTP_POST_VARS ['phone'];
$ asuburb = $ HTTP_POST_VARS ['郊区];
$ aenquiry = $ HTTP_POST_VARS ['inquiry'];
mail($ arecipient,$ asubject,
=================== ======================
请注意:这是从网站生成的电子邮件
==
名称:$ aname
电子邮件:$ aemail
电话:$ atelephone
郊区:$ asuburb
消息:
$ aenquiry
= =============================,FROM:$ asender);
header('Location:/thank-you.php');
?>
你在正确的轨道上。最后一步是在最后一条消息中输入一个字符串:
$ _ POST ['phone']?
$ telephoneString =电话:$ _ POST ['phone']。\\\
:
$ telephoneString =;
($ code> \\\
的字符串形成一个换行符。)
然后,输出消息中的字符串。它将为空,否则。
foo bar baz
========== =================================。
$ nameString。
$ emailString。
$ telephoneString。
$ downtownString;
修改
<对于个人表单域,这可能会更好。然而,为了优雅,我更喜欢@mazzzzzz的解决方案。
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).
<?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.
这篇关于PHP:不要将空白的表单字段邮寄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!