根据表格中的选择选项发送电子邮件差异人士

时间:2016-03-04 07:36:03

标签: php forms email header switch-statement

我试图制作一个表单,根据表单上选择的内容向某人发送电子邮件。当我提交表格时,我只得到一个空白页面,其中的网址指向" ../ 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');

}
}

1 个答案:

答案 0 :(得分:0)

既然已经编辑了原始问题以删除前面提到的有问题的代码片段,那么代码失败的原因仍然不明显,但可能使用下面的压缩代码。

我唯一能看到的是你明确测试了$_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>