【问题标题】:Redirect to another page after successfully submit the form成功提交表单后重定向到另一个页面
【发布时间】:2015-07-15 09:15:56
【问题描述】:

我找到了一个要编辑的模板,并且它已经有一个漂亮的联系表单。点击提交按钮后,我收到了一条感谢消息,但我想重定向到另一个页面,该页面将显示该消息,以便我可以将其用于转化跟踪。

如果我当前的代码如下所示,我应该怎么做:

<?php


        $firstname = '';
        $number = '';
        $message = '';
        $email = '';


        if($_POST) {
          // collect all input and trim to remove leading and trailing whitespaces
          $firstname = trim($_POST['first_name']);
          $number = trim($_POST['phone']);
          $message = trim($_POST['message']);
          $email = trim($_POST['email']);


          $errors = array();

          // Validate the input
          if (strlen($firstname) == 0)
            array_push($errors, "Please enter your name");

         if (strlen($number) == 0) 
            array_push($errors, "Please specify your number");

          if (strlen($message) == 0) 
            array_push($errors, "Please enter the details of your message");

          if (!filter_var($email, FILTER_VALIDATE_EMAIL))
            array_push($errors, "Please specify a valid email address");


          // If no errors were found, proceed with storing the user input
          if (count($errors) == 0) {

                //completion message into array


                $to  = 'me@mymailzz.com'; // note the comma

                // the email subject ( modify it as you wish )
                $subject = "Enquiry From website";
                // the mail message ( add any additional information if you want )


                $msg = "Quote From website:\n-------------------------------------------\n Name: $firstname \n Number: $number \n Email: $email \n Message: $message \n-------------------------------------------";    
                //function to send email

                    try {
                        mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
                        $return1 = "Thank You for sending your message, we will be in touch within 48-72 hours";


                        array_push($errors, $return1);

                        $formsubmit = 1;

                    }catch(Exception $e){
                        $return3 = "<center><p><b>Your message did not get sent due to an error. Please try again. Caught Exception {$e->getMessage()}</center>";


                        array_push($errors, $return3); 
                        $formsubmit = 0;

                    }

          }

          //Prepare errors for output
          $output = '';
          foreach($errors as $val) {
            $output .= "<p class='output'>$val</p>";
          }



        }
?>

【问题讨论】:

  • 我只是来到这个页面来支持这个问题,因为反对票不好(我什至没有阅读这个问题)

标签: php redirect


【解决方案1】:

换行:

$return1 = "Thank You for sending your message, we will be in touch within 48-72 hours";

这样的:

header('Location: some_page.php');
exit;

然后您可以在这些行过时时删除它们:

array_push($errors, $return1);

$formsubmit = 1;

这将重定向到some_page.php,而不是打印该消息。

【讨论】:

    【解决方案2】:

    要跨页面存储消息,请在脚本开头执行以下操作:

    session_start();
    

    不是将消息存储在普通变量中,而是将其存储在会话变量中,例如:

    $_SESSION['message'] = 'Your message here';
    

    在脚本的结尾:

    if ($formsubmit == 1) {
        header('Location: your-file.php');
    }
    

    your-file.php:

    <?php
    session_start();
    echo $_SESSION['message'];
    

    【讨论】:

      【解决方案3】:

      刚刚更改了您的尝试代码并插入重定向功能

      try {
               mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
               echo ("<SCRIPT LANGUAGE='JavaScript'>
                  window.location.href='next_page.php'
                    </SCRIPT>");
      }
      

      【讨论】:

        【解决方案4】:

        您如何在您拥有 &lt;form action="#"&gt; 的 Web 模板中进行跟踪,并从以前发现的类似问题 here 的帮助中继续进行操作

        【讨论】:

          猜你喜欢
          • 2017-06-26
          • 1970-01-01
          • 1970-01-01
          • 2013-06-14
          • 1970-01-01
          • 1970-01-01
          • 2022-01-20
          • 1970-01-01
          相关资源
          最近更新 更多