首页 > 解决方案 > 寻找表单邮件脚本的 PHP 重定向代码

问题描述

我正在使用“ReusableForms”中的表单,它工作正常,但由于 Bootstrap 版本冲突,我无法将表单包含在我的 Bootstrap 4.2.1 网页中,我必须将表单作为“自己的”页面使用这很好,但是在提交表单并显示成功消息后,我希望表单重定向到我网站的主页。这是处理程序和 html 表单的代码:

<form method="post" id="reused_form" >
                    <button class="button-primary" type="submit">Submit</button>
                </form>
                <div id="success_message" style="display:none">
                    <h3>Submitted the form successfully!</h3> 
                    <p> Eric will get back to you soon. </p>
                </div>
                <div id="error_message" style="width:100%; height:100%; display:none; "> <h3>Error</h3> Sorry there was an error sending your form. </div>

php代码:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/*
Tested working with PHP5.4 and above (including PHP 7 )

 */
require_once './vendor/autoload.php';

use FormGuide\Handlx\FormHandler;


$pp = new FormHandler(); 

$validator = $pp->getValidator();
$validator->fields(['Name','Email'])->areRequired()->maxLength(50);
$validator->field('Email')->isEmail();
$validator->field('Message')->maxLength(6000);


$pp->requireReCaptcha();
$pp->getReCaptcha()->initSecretKey('');

$mailer = $pp->getMailer();

//Using SMTP account
$mailer->IsSMTP();
$mailer->SMTPAuth   = true;
$mailer->SMTPSecure = "tls";
$mailer->Host       = "";
$mailer->Username   = "";
$mailer->Password   = "";

$mailer->setFrom('', 'Camp Bay Form');


$pp->sendEmailTo(''); // ← Your email here

echo $pp->process($_POST);

我一直在搜索互联网,尝试处理程序中的其他代码,我尝试了隐藏的重定向输入,但没有任何效果,我认为处理程序代码的编写方式与那里的其他代码不匹配。

我希望在处理程序代码中添加一些非常容易的东西,我只需要知道在其中放入什么才能使重定向工作!

标签: phpformsemail

解决方案


请更具体地回答您的问题。据我所知,我看到您需要 HTML 表单中的目的地。

将 action="example.com/index.php" 添加到您的 HTML 中。

<form method="post" id="reused_form" action="/handler.php">
<button class="button-primary" type="submit">Submit</button>
</form>
<div id="success_message" style="display:none">
<h3>Submitted the form successfully!</h3> 
<p> Eric will get back to you soon. </p>
</div>
<div id="error_message" style="width:100%; height:100%; display:none; "> <h3>Error</h3> Sorry there was an error sending your form. </div>

虽然我不建议这样做。最好将所有内容都放在同一个文件中:

    ```
    <?php
if(isset($_POST)) {
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    /*
    Tested working with PHP5.4 and above (including PHP 7 )

     */
    require_once './vendor/autoload.php';

    use FormGuide\Handlx\FormHandler;


    $pp = new FormHandler(); 

    $validator = $pp->getValidator();
    $validator->fields(['Name','Email'])->areRequired()->maxLength(50);
    $validator->field('Email')->isEmail();
    $validator->field('Message')->maxLength(6000);


    $pp->requireReCaptcha();
    $pp->getReCaptcha()->initSecretKey('');

    $mailer = $pp->getMailer();

    //Using SMTP account
    $mailer->IsSMTP();
    $mailer->SMTPAuth   = true;
    $mailer->SMTPSecure = "tls";
    $mailer->Host       = "";
    $mailer->Username   = "";
    $mailer->Password   = "";

    $mailer->setFrom('', 'Camp Bay Form');


    $pp->sendEmailTo(''); // ← Your email here

    $output = $pp->process($_POST);
}
    ?>
    <form method="post" id="reused_form" >
                        <button class="button-primary" type="submit">Submit</button>
                    </form>
    <?php echo $output; ?>
    ```

推荐阅读