Daniel Zawadzki
Daniel Zawadzki

Reputation: 179

Return to the previous page and the message

I have a problem because I'm new to PHP. I wrote a simple contact form, but after sending a message (everything works fine), it takes me to a blank page. Is there any way to go back to the contact.html page and show the message "sent successfully"?

<?php
    if(isset($_POST['submit'])) {
        $to ='[email protected]';
        $subject =$_POST['subject']; 
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
        $message = "
                <html>
                    <head>
                        <title>$_POST['subject']</title>
                    </head>
                    <body>
                        <b>Person: </b>    ".$_POST['name']." </br>
                        <b>Email: </b>     ".$_POST['email']." </br>
                        <b>Message: </b>   ".$_POST['detail']." </br>
                    </body>
                </html>" ;
     mail($to, $subject, $message, $headers);
    }
?>

Upvotes: 0

Views: 6847

Answers (4)

Bhavin Rana
Bhavin Rana

Reputation: 1582

you can not get the Query string in html page so you need to convert it as php page

    <?php


    header('Location: contact.php?msg=sent successfully');
    ?>

contact.php page 

    <?php

    if($_GET['msg'])
    { ?>
    <p>
    Message has been sent successfully.
    thank you.

    </p>

<?php }

 ?>

Other Solution :

make a new page Thankyou.html for message and use this

<?php


header('Location: Thankyou.html');
?>

Upvotes: 0

William Buttlicker
William Buttlicker

Reputation: 6000

In your HTML file you cant use PHP so First change contact.html to contact.php , then :

try this:

if (mail($to, $subject, $message, $headers)) {
 header("location:contact.php?msg=sent successfully");
}

In contact.php

if(isset($_GET['msg'])) {
 echo $_GET['msg']
}

Upvotes: 0

Rikesh
Rikesh

Reputation: 26441

You can use header function to redirect.

header('Location: contact.php?msg=Mail Sent');
exit;

contact.php

if(isset($_GET['msg'])){
    echo $_GET['msg'];
}

Upvotes: 1

Praveen kalal
Praveen kalal

Reputation: 2129

header("location:contact.php?msg=YOUR_MSG");
die();

Upvotes: 0

Related Questions