如果用户没有选择然后表单发送电子邮件,但在电子邮件中它不应该有此字段

时间:2013-09-24 20:20:42

标签: php jquery html forms email

我已将此表单发送至: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

3 个答案:

答案 0 :(得分:3)

您需要在客户端上解决此问题,如下所示: - 在第一个项目上使用空白值

<select>
    <option value="">Select an item</option>
    .... other options...
</select>

答案 1 :(得分:0)

只需检查字段的值是否为&#34;选择&#34;。

foreach ($_POST as $Field=>$Value) { 
if($Value != '' && $Value != 'Select' ){
$body .= "$Field: $Value\n";
}
}

答案 2 :(得分:0)

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

// 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 );