首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在php中使用javascript将用户重定向到另一个页面?

如何在php中使用javascript将用户重定向到另一个页面?
EN

Stack Overflow用户
提问于 2018-08-26 13:15:50
回答 1查看 3K关注 0票数 0

我只想在显示成功消息后将用户重定向到另一个页面。下面的代码重定向到另一个页面,即使该页面在未提交任何数据的情况下被刷新。

代码片段

<?php
// We will store our status message later
$message = '';

// Let's check form submitted
if( isset( $_POST['action'] ) && $_POST['action'] == 'submit' ) {

    $name = $_POST['name']; 
    $email = $_POST['email']; 
    $phone = $_POST['number'];

    $message_text = $_POST['address'];


        if (isset($_POST['action'])) 
        {   

            // Validate required fields 
            if( $name == '' || $email == '' || $message_text == '' ){
                $message = "<p class=\"error\">Please fill out all required fields.</p>";  
            }


            // send email after validation
            else {

                // Email will be sent at this email
                $mailto = 'nepstarditsolutions@gmail.com';

                // Let's create html email format 
                // (You can use html in this email format)

                // Email headers
                $headers = "From: " . strip_tags( $email ) . "\r\n";
                $headers .= "Reply-To: ". strip_tags( $email ) . "\r\n";
                $headers .= "MIME-Version: 1.0\r\n";
                $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

                // Email body
                $message = "<html><body>";
                $message .= "<h3>Contact Info: </h3>";
                $message .= "<p><strong>Name: </strong> ". strip_tags( $name ) . "</p>\r\n";
                $message .= "<p><strong>Email: </strong> ". strip_tags( $email ) . "</p>\r\n";

                $message .= "<p><strong>Contact Number: </strong> ". strip_tags( $phone ) . "</p>\r\n";
                $message .= "<p><strong>Alternate Email: </strong> ". strip_tags( $_POST['altemail'] ) . "</p>\r\n";
                $message .= "<p><strong>Hosting Package: </strong> ". strip_tags( $_POST['cradio'] ) . "</p>\r\n";

                $message .= "<p><strong>Address: </strong>". strip_tags( $message_text ) . "</p>\r\n";
                $message .= "</body></html>";



                // Email subject
                $subject = "Contact Info from ".$name;

                // Send email
                $mail_status = @mail( $mailto, $subject, $message, $headers );

                if( $mail_status ){
                    $message = '<div class="alert alert-success">Quotation submitted Successfully ! </div>';   
                }
                else {
                    $message = '<div class="alert alert-danger">Error in sending the Quotation ! </div>';  
                }

            }
        }   
}
?>

我在2件事上需要帮助

  1. 成功消息应该显示2秒(在重定向之前)。
  2. 如何在php中使用JS重定向?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-26 13:38:21

重定向页面的标准“普通”JavaScript方法

您可以使用下面的代码在表单提交代码之后重定向用户。

if( $mail_status ){
    echo "
        <script>
            setTimeout(function() {
                window.location = 'http://www.example.com/newlocation';
            }, 2000);
        </script>
    ";
}

关于setTimeout工作原理的MDN web docs

希望它能帮上忙!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52023393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档