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

时间:2018-08-26 05:15:50

标签: javascript php html url-redirection

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

代码段

<?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>';  
                }

            }
        }   
}
?>

我需要两点帮助

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

1 个答案:

答案 0 :(得分:0)

标准的“香草” JavaScript重定向页面的方式

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

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

MDN web docs关于setTimeout的工作方式

希望有帮助!