doushi5024 2018-08-26 05:15
浏览 92
已采纳

如何使用PHP中的JavaScript将用户重定向到另一个页面?

I want to redirect the user to another page only after displaying a success message. The code below redirects to the another page even if the page is refreshed without submitting any data.

Code Snippet

<?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 ) . "
";
                $headers .= "Reply-To: ". strip_tags( $email ) . "
";
                $headers .= "MIME-Version: 1.0
";
                $headers .= "Content-Type: text/html; charset=ISO-8859-1
";

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

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

                $message .= "<p><strong>Address: </strong>". strip_tags( $message_text ) . "</p>
";
                $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>';  
                }

            }
        }   
}
?>

I need help in 2 things

  1. Success message should be displayed for 2 seconds (before the redirect).
  2. How to redirect using JS in php?
  • 写回答

1条回答 默认 最新

  • duanmian1085 2018-08-26 05:38
    关注

    Standard "vanilla" JavaScript way to redirect a page

    You can use the code below to redirect the user after form submission code.

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

    MDN web docs on how setTimeout works

    Hope it helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题