只发送已填写的字段框。如果为空,则不要发送电子邮件。php表单到电子邮件
我有这个简单的表单可以用php发送电子邮件。在send php代码中,此表单的优点在于,我不必写出所有字段的名称。例如,我不必这样写:只发送已填写的字段框。如果为空,则不要发送电子邮件。php表单到电子邮件,php,html,forms,email,post,Php,Html,Forms,Email,Post,我有这个简单的表单可以用php发送电子邮件。在send php代码中,此表单的优点在于,我不必写出所有字段的名称。例如,我不必这样写: $msg .= "$_POST[ContactName] Contact Name: ContactName\n" 在我的代码中,我要写的就是: foreach ($_POST as $Field=>$Value) { $body .= "$Field: $Value\n"; 该表单目前运行良好。它目前正在发送: ContactNam
$msg .= "$_POST[ContactName] Contact Name: ContactName\n"
在我的代码中,我要写的就是:
foreach ($_POST as $Field=>$Value) {
$body .= "$Field: $Value\n";
该表单目前运行良好。它目前正在发送:
ContactName: name here
BusinessName: business name here
EmailAddress: email@email.com
Email_Confirmation:
PhoneNumber: 714-555-5555
但是,我希望发生这种情况:如果某个字段留空,或者web用户没有将其填充到某个字段框中,则表单不应发送此字段。例如:web用户决定不填写ContactName和/或BusinessName。因此,表单应仅发送以下格式:
EmailAddress: email@email.com
Email_Confirmation:
PhoneNumber: 714-555-5555
注意到没有提到ContactName:和BusinessName:
请帮忙!我将不胜感激-米歇尔哈
以下是php发送代码:
if the Email_Confirmation field is empty
if(isset($_POST['Email_Confirmation']) && $_POST['Email_Confirmation'] == ''){
// put your email address here
$youremail = 'bomandty@gmail.com';
// prepare a "pretty" version of the message
$body .= "Thank you for your request. We will get back with you soon.";
$body .= "\n";
$body .= "\n";
foreach ($_POST as $Field=>$Value) {
$body .= "$Field: $Value\n";
$body .= "\n";
}
$CCUser = $_POST['EmailAddress'];
// Use the submitters email if they supplied one
// (and it isn't trying to hack your form).
// Otherwise send from your email address.
if( $_POST['EmailAddress'] && !preg_match( "/[\r\n]/", $_POST['EmailAddress']) ) {
$headers = "From: $_POST[EmailAddress]";
} else {
$headers = "From: $youremail";
}
// finally, send the message
mail($youremail, 'subject line here', $body, $headers, $CCUser );
}
// otherwise, let the spammer think that they got their message through
将支票添加到您的帐户中
if(empty($Value)) continue;
并将表单中的默认值更改为占位符
if(empty($Value)) continue;
foreach ($_POST as $Field=>$Value) {
if($Value != ''){
$body .= "$Field: $Value\n";
}
}
并将表单中的默认值更改为占位符是否使用empty()函数?如果(!empty($Value)){//继续追加消息},是否使用empty()函数?如果(!empty($Value)){//继续追加消息}现在您的代码正在工作,很好。现在,我有一个新问题。请看这里:注意我有一堆菜单列表下拉选择不同的大小,等等。最初的选择总是“选择”。当我发送测试时,它发送所有字段。如果用户没有选择任何内容,或者默认情况下“选择”它不会在我的电子邮件中发送,我如何修改表单代码。表单将发送,但在电子邮件中,我不会得到任何带有“Select”的字段。这是我的代码:foreach($\u POST as$Field=>$Value){if($Value!=''){$body.=“$Field:$Value\n”;现在你的代码正常工作了,很好。现在,我有一个新问题。请看这里:注意,我有一堆菜单列表下拉列表,用于选择不同的大小,等等。初始选择总是“选择”。当我发送测试时,它发送所有字段。如果用户没有选择任何内容或默认为“选择”,我如何修改表单代码它不会在我的电子邮件中发送。表单会发送,但在电子邮件中,我不会得到任何带有“选择”的字段。这是我的代码:foreach($\u POST as$Field=>$Value){if($Value!=''){$body.=“$Field:$Value\n”;
foreach ($_POST as $Field=>$Value) {
if($Value != ''){
$body .= "$Field: $Value\n";
}
}