自定义PHP表单需要刷新才能正确加载页面

时间:2021-02-23 作者:Sam Donaghy-Bell

我有一个包含所有内容的单页网站。我仅有的另一页是“a”;感谢您提交联系表“;页在此“中”;感谢提交。php“;页面我有我的PHP表单提交代码。

我是wordpress的新手,所以我可能完全错了,但我已经为此创建了一个自定义页面模板;感谢提交。php“;使用模板名称的页面:谢谢提交。

然后,我将该模板应用于WP中的一个全新页面。我无法添加。php到slug,这可能是唯一的问题,但我看不到解决方法。一个奇怪的问题是,当我提交表单时,我找到了正确的URL,但得到了一个;“找不到页面”;错误如果我复制并粘贴这个完全相同的URL,我会转到正确的页面。。。

下面是所有代码。

我看了一下,我看不出这个问题是重复的,但如果是这样的话,请朝着正确的方向轻推一下,我将不胜感激!

类型

<form method="post" action="thankssubmit.php">
        <h3>Drop Us a Message</h3>
        <div class="row">

          <div class="col-sm">
            <div class="form-group">
              <input id="nameInput" type="text" name="name" onkeyup="manage(this)" class="form-control" placeholder="Your Name *" />
            </div>
            <div class="form-group">
              <input id="emailInput" type="text" name="email" onkeyup="manage(this)" class="form-control" placeholder="Your Email *" />
            </div>
            <div class="form-group">
              <input type="text" name="phone" class="form-control" placeholder="Your Phone Number" />
            </div>
            <div class="form-group">
              <input id="weddingDatePicker" type="date" name="date" class="form-control" placeholder="Your Wedding Date -" />
            </div>
            <div class="form-group">
              <input type="text" name="hear" class="form-control" placeholder="How did you hear about us?" />
            </div>
          </div>

          <div class="col">
            <div class="form-group">
              <textarea id="messageInput" name="message" onkeyup="manage(this)" class="form-control" placeholder="Your Message *" style="width: 100%; height: 254px;"></textarea>
            </div>

            <div class="form-group">
              <input id="contactSubmitBtn" type="submit" disabled name="btnSubmit" class="btn" value="Send Message" />
            </div>
          </div>
        </div>
      </form>
感谢提交。php页面

<?php get_header(); ?>

<!-- THANKS OR SUCCESS MESSAGE AFTER THE EMAIL HAS BEEN SENT -->
<section id="thanksMsgPG">

<h1 class="thanksmsg">Thanks for getting in touch.</h1>
<h3 class="thanksmsg">We\'ll get back to you ASAP!</h3>
<a href="/"><button class="btn ">Back</button></a>

</section>

<?php get_footer(); ?>


<?php

/*
Template Name: Thanks Submit
*/


if (isset($_POST[\'btnSubmit\'])) {

    // EMAIL AND SUBJECT OF EMAIL BEING SENT
    $email_to = "[email protected]";
    $subject = "Contact Submission Form";

//ERROR MESSAGES IF DIED FUCNTION IS CALLED (SEMI-REDUNDANT BECAUSE SEND BUTTON WONT BE ENABLED UNTIL INPUT FIELDS ARE FILLED CORRECTLY ANYWAY)
    function died($error)
    {

        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error . "<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // IF NOTHING ENTERED THEN THROW ERROR MESSAGE
    if (!isset($_POST[\'name\']) ||
        !isset($_POST[\'email\']) ||
        !isset($_POST[\'message\'])) {
        died(\'We are sorry, but there appears to be a problem with the form you submitted.\');
    }

//GETTING THE NAME EMAIL PHONE MESSAGE FROM THE FORM AND PUTTING IT INTO VARIABLES
    $full_name = $_POST[\'name\']; // required
    $email_from = $_POST[\'email\']; // required
    $phone = $_POST[\'phone\'];
    $date = $_POST[\'date\'];
    $hear = $_POST[\'hear\']; // required
    $message = $_POST[\'message\']; // required


    $error_message = "";
    $email_exp = \'/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$/\';

//CHECKING TO MAKE SURE VALID EMAIL IS ENTERED
    if (!preg_match($email_exp, $email_from)) {
        $error_message .= \'The e-mail you entered does not appear to be valid.<br />\';
    }
//CHECKING TO MAKE SURE VALID NAME IS ENTERED
    $string_exp = "/^[A-Za-z .\'-]+$/";
    if (!preg_match($string_exp, $full_name)) {
        $error_message .= \'The name you entered does not appear to be valid.<br />\';
    }
//CHECKING TO MAKE SURE MESSAGE IS MORE THAN 2 CHARACTERS
    if (strlen($message) < 2) {
        $error_message .= \'The message you entered does not appear to be valid.<br />\';
    }
    if (strlen($error_message) > 0) {
        died($error_message);
    }

    $email_message = "Form details below.\\n\\n";

//MAKING SURE THERE ARE NO HEADER INJECTIONS
    function clean_string($string)
    {
        $bad = array("content-type", "bcc:", "to:", "cc:", "href");
        return str_replace($bad, "", $string);
    }

//THE FORMAT OF THE EMAIL BEING SENT. CLEAN STRING CLEANING ANY WHITE SPACE
    $email_message .= "Name: " . clean_string($full_name) . "\\n";
    $email_message .= "Email: " . clean_string($email_from) . "\\n";
    $email_message .= "Phone: " . clean_string($phone) . "\\n";
    $email_message .= "Wedding Date: " . clean_string($date) . "\\n";
    $email_message .= "I heard about you via: " . clean_string($hear) . "\\n";
    $email_message .= "Message: \\r\\n" . clean_string($message) . "\\n";
    $email_from = $full_name . \'<\' . $email_from . \'>\';

// CREATING EMAIL HEADER FOR GMAIL TO RECOGNISE
    $headers = \'From: \' . $email_from . "\\r\\n" .
    \'Reply-To: \' . $email_from . "\\r\\n" .
    \'X-Mailer: PHP/\' . phpversion();
    @mail($email_to, $subject, $email_message, $headers);

    // echo $email_message;

    ?>

<?php
}
?>
下面的第一幅图显示了我们提交表单后的URL错误,第二幅图显示了我们重新加载页面URL后的URL错误。After click submit button on previous page

After reload of URL

2 个回复
SO网友:Amir Espelani

为什么不使用WordPress插件?尝试以下插件:

重力表联系表7和contact Form 7 Thank You

SO网友:Amir Espelani

感谢您的评论

您需要更改部分代码

这条路不太对,但行得通

在目录中wp-content/themes/your-theme
装箱一个php文件并设置名称page-thanks.php

并将您的代码放入此文件中,以实现以下目的:

<?php
     /**
     * Template Name: thank you page
     *
     */
     get_header();

     YOUR CODE 

     get_footer();
      ?>
现在创建一个页面并设置页面模板谢谢页面

现在您可以看到您创建的页面