为什么我的<select>值没有在电子邮件中发布</select>

时间:2012-03-03 18:52:39

标签: php html email

以下是我正在使用的表单:

<form action="scripts/contactscript.php" method="post" id="contactform">
  <ol>
    <li>
      <label for="name">Your Name <span class="red">*</span></label>
      <input id="name" name="name" class="text" />
    </li>
    <li>
      <label for="email">Your email <span class="red">*</span></label>
      <input id="email" name="email" class="text" />
    </li>
    <li>
      <label for="interested">Interested In <span class="red">*</span></label>
      <select id="interested" name="interested">
        <option value="">-- Please Select --</option>
        <option value="GeneralInformation">General Information</option>
        <option value="PurchasingABike">Purchasing A Bike</option>
      </select>
    </li>
    <li>
      <label for="message">Message <span class="red">*</span></label>
      <textarea id="message" name="message" rows="6" cols="50"></textarea>
    </li>
    <li class="buttons">
      <input type="image" name="imageField" id="imageField" src="images/send.gif" class="send" />
      <div class="clr"></div>
    </li>
  </ol>
</form>

这是它发布的脚本:

<?php
if(!$_POST) exit;
$email = $_POST['email'];

//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
    $error.="Invalid email address entered";
    $errors=1;
}

if($errors==1) echo $error;
else{
    $values = array ('name','email','message','interested');
    $required = array('name','email','message');

    $your_email = "jon.harding@webedgemedia.com";
    $email_subject = "Little Bike Riders Message: ".$_POST['subject'];
    $email_content = "New message:\n\n";

    foreach($values as $key => $value){
      if(in_array($value,$required)){
          if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
      }
        $email_content .= $value.': '.$_POST[$value]."\n\n" ;
    }


    if(@mail($your_email,$email_subject,$email_content)) {
        echo 'Message sent!'; 
    } else {
        echo 'ERROR!';
    }
}
?>

以下是电子邮件发送时显示的内容:

新消息:

姓名:Jon Harding

电子邮件:jon@email.com

消息:测试

感兴趣的是:

1 个答案:

答案 0 :(得分:1)

在提交具有选项值-- Please Select --并且正在电子邮件中显示的表单时,您已选择默认条目""。我在本地尝试了你的例子,如果我选择其中一个选项,它就可以了。

修改

您正在使用以下代码提交表单:

jQuery(document).ready(function(){
    $('#contactform').submit(function(){                  
        var action = $(this).attr('action');
        $.post(action, { 
            name: $('#name').val(),
            email: $('#email').val(),
            subject: $('#subject').val(),
            message: $('#message').val()
        },
            function(data){
                $('#contactform #submit').attr('disabled','');
                $('.response').remove();
                $('#contactform').before('<p class="response">'+data+'</p>');
                $('.response').slideDown();
                if(data=='Message sent!') $('#contactform').slideUp();
            }
        ); 
        return false;
    });
});

您忘记将感兴趣的元素interested: $('#interested').val()的值添加到要传递给$ .post方法的对象。