Submit and return form a php script to main page after 5 seconds

Posted on 2004-11-21
Last Modified: 2010-05-18
Hi all,

I have a script for which I would like to submit a form to.  But what I would like to do is to process the script and print out a message lets say

echo "Thank you! You will now be returned to the main page";

Is there a way of doing this so that the script page is displayed for lets say 5 seconds and is then redirected back to the page from which it came.  Not to go back to the page rather to be sent to the url of the page.

Many thanks

Question by:pjcrooks2000
    LVL 27

    Accepted Solution

    First thing to tackle:

    >> redirected back to the page from which it came

    There are several approaches to this, the most convienient (but not the most reliable) is the http referer. This is perfect in every sense accept the fact that sometimes it wont be set, in these cases you can add a default page to load if it isn't set.


    $referer = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : "";

    Another approach is using sessions to track the page activity, on each page you shift the value of a current page variable into a previous page variable (if the first is set) and then assign the current page (using $_SERVER['PHP_SELF']) into the current page variable. This allows you to be able to obtain the last visited page (on your server only) to redirect to but seems a bit over complicated for this.

    The last method is to append the current page to the query string in the form tag, eg:

    <form method="post" action="page.php?ref=<?php echo $_SERVER['PHP_SELF']; ?>"> etc...


    $referer = (isset($_GET['ref'])) ? $_GET['ref'] : "";

    Again we have a default page incase the query string isn't set.

    Then we have the actual redirect, usually for redirecting we use:


    However this is instant, as you need the delay i would use the meta refresh html tag. (In this example i have also included the HTTP_REFERER redirect method):

    //process data

    $referer = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : "";

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="refresh" content="5;URL=<?php echo $referer; ?>">



    Thank you message here

    LVL 8

    Author Comment

    Once again Diablo fantastic answer :)

    I guess I will have more for you soon ;)

    LVL 27

    Expert Comment

    no problem as always :)

    If you ever have anything come up relating to something you've already asked feel free to post back in the same thread even if it's PAQ'd. Good luck with your code.

    LVL 8

    Author Comment

    Thanks DIablo your great :)

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now