PHP表单操作-根据表单选择发布到PHP文件吗?

时间:2019-10-01 21:47:16

标签: php

我有一个标准的PHP表单。提交表单后,PHP“邮件程序”文件向用户发送“谢谢”电子邮件,并将用户重定向到“感谢”页面。我想在表单中添加一个下拉菜单,并与每个下拉选项关联不同的“邮件程序”文件。换句话说,选择的下拉选项确定使用哪个“邮件程序”文件...每个邮件程序文件具有不同的“谢谢”电子邮件文本和不同的“谢谢”页面URL。可以使用PHP完成此操作,还是需要自定义脚本?

作为参考,我已经包括了表单文件和邮件文件的相关部分:

PHP表格

    <form  action="https://example.com/mailer.php" method="post" >

    <label for="name">
    <span>Name:</span>
    <input type="text" id="name"  name="name">
    </label>

    <label for="dropdown">
    <span>Which of the following:</span>
    <select id="dropdown" name="dropdown"  >
    <option label=""></option>
    <option value="Option 1">Option 1</option>
    <option value="Option 2">Option 2</option>
    <option value="Option 3">Option 3</option>
    </select>
    </label>

    <label for="email">
    <span>E-mail:</span>
    <input type="text" id="email"  name="email">
    </label>

    <button type="submit" name="submit" id="submit"><b>SUBMIT</b></button>
    </form>

PHP邮件文件

    // FORM SUBMISSION EMAIL ADDRESS
    $mailto = 'example@example.com' ;

    // FORM SUBMISSION EMAIL SUBJECT
    $from = "subject" ;

    // FORM SUBMISSION THANK YOU PAGE REDIRECT */
    $thankyouurl = "https://example.com/thanks.php" ;

    $message =

    // FORM FIELDS
    "Name: $name\n\n" .
    "Dropdown: $dropdown\n\n" .
    "E-mail: $email\n\n" . ;

    mail($mailto, $from, $message,
    "From: <$mailto>" . $headersep . "Reply-To: <$mailto>" . $headersep );

    // AUTORESPONDER SENDER NAME AND EMAIL 
    $myname = "Organization";
    $mymail = "noreply@example.com";

    // AUTORESPONDER SUBJECT 
    $respond_subject = Subject";

    // AUTORESPONDER MESSAGE
    $respond_message = "Message
    ";

    $headers = "Content-Type: text/html; charset=utf-8\nFrom: $myname <$mymail>\nReply-To: 
    <$mymail>\nReturn-Path: <$mymail>\nX-Mailer: PHP";

    // THIS IS THE AUTORESPONDER
    mail($email, $respond_subject, $respond_message, $headers);

    header( "Location: $thankyouurl" );

1 个答案:

答案 0 :(得分:0)

我在Stack上发现了类似的帖子,其中包含我成功使用的解决方案。

PHP表格

     <script>
     function chgAction(){
     $('#form1').attr({'action':$('option:selected').attr('data-action')});
     $('#form1').submit();
     }
     </script>

     <form  id="form1" name="form1" action="/" method="post" >

     <label for="dropdown">
     <span>Which of the following:</span>
     <select id="dropdown" name="dropdown" onChange="javascript:chgAction()"  >
     <option label=""></option>
     <option data-action="https://example.com/mailer.php" value="Option 1">Option 1</option>
     <option data-action="https://example.com/mailer2.php" value="Option 2">Option 2</option>
     <option data-action="https://example.com/mailer3.php" value="Option 3">Option 3</option>
     </select>
     </label>