0

我有这个表格要通过电子邮件发送: http: //test4.bycustomdesign.com/order-form.shtml

表格效果很好。注意到我有一堆菜单列表选择框来选择不同的大小等。默认的初始选择始终是“选择”

我希望:如果用户没有选择任何内容,则可以发送表单。但是,在我收到的电子邮件中,如果用户没有选择任何内容,那么它就不会出现在电子邮件中。或者,如果它保留为默认的“选择”,那么它不应该出现在电子邮件中。因为并非所有字段都被选中,并且在电子邮件中我会得到一个很长的所有字段列表“选择”我希望通过只发送用户选择的任何内容来缩短它,而不是所有“选择”

请帮忙!谢谢你。米歇尔哈。这是我的代码:

// if the Email_Confirmation field is empty
if(isset($_POST['Email_Confirmation']) && $_POST['Email_Confirmation'] == ''){

// put your email address here
$youremail = 'bomandty@gmail.com, ryanjiles@betadiamond.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) { 
    if($Value != ''){
            $body .= "$Field: $Value\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, 'Request from BetaDiamonds.com', $body, $headers, $CCUser );
}
// otherwise, let the spammer think that they got their message through
4

3 回答 3

3

您需要像这样在客户端上解决此问题: - 在第一项上使用空白值

<select>
    <option value="">Select an item</option>
    .... other options...
</select>
于 2013-09-24T20:25:09.210 回答
0

只需检查该字段的值是否不是“选择”。

foreach ($_POST as $Field=>$Value) { 
if($Value != '' && $Value != 'Select' ){
$body .= "$Field: $Value\n";
}
}
于 2013-09-24T20:24:49.913 回答
0

在我看来,您可以将“选择”添加到它跳过的值中。您还可以将 Select 选项值设置为“”,但此时将代码更改为以下内容会更容易。

// put your email address here
$youremail = 'bomandty@gmail.com, ryanjiles@betadiamond.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) { 
    if($Value != '' && $Value != "Select"){
        $body .= "$Field: $Value\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, 'Request from BetaDiamonds.com', $body, $headers, $CCUser );
于 2013-09-24T20:25:41.737 回答