根据表单中的选择选项向不同的人发送电子邮件


email differant person depending on select option in form

我试图制作一个表单,根据表单上的选择向某人发送电子邮件。 当我提交表格时,我只得到一个空白页,网址指向"../wp-content/test/test.php"它甚至不会进入默认的 oops 页面。我不确定我做错了什么。任何意见都值得赞赏。在我添加开关之前,一切正常。我可以向一个人发送电子邮件,以获得所有选项就好了。这是我的表格:

<form name="contactform" method="post" action="../wp-content/test/test.php">


 <input  type="text" name="name" maxlength="50" size="30">
 <input  type="text" name="email" maxlength="80" size="30">
<select id="club" name="club">
  <option name="wordone" value="wordone">wordone</option>
  <option name="wordtwo" value="wordtwo">wordtwo</option>
</select>
  <input type="submit" value="Submit">  
</form>  

这是我的PHP:

   <?php
if(isset($_POST['Submit'])){
    $name = $_POST['name'];
    $club = $_POST['club'];
    $email_from = $_POST['email'];
    $email_subject = 'New Form submission';
    $email_body = '...';
    $headers = "From: $email_from 'r'n";
    $headers .= "Reply-To: $email_from 'r'n";
switch($_POST['club']){
case 'wordone':
    $to = "nick@website.com";
    mail($to,$email_subject,$email_body,$headers);
    header('Location: http://website.com/thanks2');
break;
case 'wordtwo':
    $to = "chris@website.com";
    mail($to,$email_subject,$email_body,$headers);
    header('Location: http://website.com/thanks1');
break;
default:
    header('Location: http://website.com/oops');
}
}

既然原始问题已被编辑以删除前面提到的有问题的代码片段,那么代码失败的原因仍然不明显,但也许以下精简代码可能会有用。

我唯一能看到的是你显式测试了$_POST['Submit']但提交按钮没有名称。

<?php
    /* check that all expected POST variables are present */
    if( isset( $_POST['submit'], $_POST['name'], $_POST['club'], $_POST['email'] ) ){
        /* set POST variables as named vars for convenience */
        $name = trim( $_POST['name'] );
        $club = trim( $_POST['club'] );
        $email= trim( $_POST['email'] );
        $php=phpversion();
        $default = false;
        /* do these change on a person by person user or stay the same? */
        $subject = 'New Form submission';
        $body = '...';

        switch( $club ){
            case 'wordone':
                $to = "chris@website.com";
                $redirect='http://website.com/thanks1';
            break;
            case 'wordtwo':
                $to = "nick@website.com";
                $redirect='http://website.com/thanks2';
            break;
            default:
                $default=true;
                $result=false;
                $redirect='http://website.com/oops';
            break;
        }

        /* send the email */
        if( !$default ){
            $headers = "From: {$email}'r'n";
            $headers .= "Reply-To: {$email}'r'n";
            $headers .= "X-Mailer: PHP/{$php}'r'n";
            $result=@mail( $to, $subject, $body, $headers );            
        }
        /* redirect */
        header( 'Location: '.$redirect.'?mailsent='.$result );
    }
?>

<form name="contactform" method="post" action="../wp-content/test/test.php">
    <input type="text" name="name" maxlength="50" size="30">
    <input type="text" name="email" maxlength="80" size="30">
    <!-- no need for the `name` attribute in an `option` and they can remain unclosed like this -->
    <select id="club" name="club">
        <option value="wordone">wordone
        <option value="wordtwo">wordtwo
    </select>
    <input type="submit" name='submit' value="Submit">  
</form>