0

Okay so I made a form on a website so that someone can write down their information and submit a message to an email. It then redirects them back to the homepage of the site, the problem I'm having is that it doesn't show when the message has sent, it just takes me back to the homepage automatically. How can I put a small delay so that it will show that the email was sent? Maybe I need to make it pop in a message box instead? It's been a while since I've worked with php so I'm a bit rusty.

<?php
    ob_start();

    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $from = 'From: Subject';
    $to = 'myemailhere@example.com';
    $subject = 'Subject';

    $body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";

    if ($_POST['submit']) {
        if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        }
    }

    $url = 'http://example.com';

    while(ob_get_status())
    {
        ob_end_clean();
    }

    header("Location: $url");
?>

Also I replaced a few things with generic information, sorry guys you're not getting my email.

SecretWalrus
  • 99
  • 1
  • 1
  • 10

2 Answers2

1

You can't easily do this on the server side. The best thing to do is return a page which contains the message, and redirects shortly after, either via javascript or META tags.

See answer at Redirect website after certain amount of time

Community
  • 1
  • 1
Daniel
  • 4,481
  • 14
  • 34
1

You can only send the redirect header before any html is output. Have you've considered using a timed meta html redirect?

<meta http-equiv="refresh" content="5; url=http://homepage.com/">

or js:

window.location.href = "http://homepage.com";

Note: You can also instead of redirecting, let the user know that the form has been submitted successfully, and link them to the page they were prior.

Eg: Thank you for your submission, click here to return to our website.

Curtis W
  • 511
  • 4
  • 11