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!

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

报告相同问题?