Topic: Redirecting a form back to the index page.

Hi

I have this php code so when you click submit it goes to a thank you page, but what i actually want is it to say thank you and go back to the index page automatically. Does anyone know how to do this. Here's the code

<?php
echo "<p>Thank you, <b>$_POST[name]</b>, Your message has been sent.</p>";
//start building the mail string
$msg = "Name; $_POST[name]\n";
$msg .= "E-Mail: $_POST\n";
$msg .= "Message: $_POST\n";
$recipient = "kreativ108@gmail.com";
$subject = "comment form";
$mailheaders = "From: My Website <you@yourdomain.com> \n";
$mailheaders .= "Reply-to: $_POST";
//send the mail
mail($recipient, $subject, $msg, $mailheaders);
?>

Vote up Vote down

Re: Redirecting a form back to the index page.

You'll need to use the header() function: http://us2.php.net/manual/en/function.header.php

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Redirecting a form back to the index page.

Hi  falkencreative,
This code will redirect a page to a given page after 5 seconds. you can increase or decrease the time interval. Put this code in the thank you page and it will redirect the page to index page automatically after 5 seconds. you can also do this using meta refresh.

<html>
<head>
<script>
function redirect(){
document.location='index.php';
}
</script>
</head>
<body onload="setTimeout('redirect()', 5000);//goes to index.php after 5 seconds">
</body>
</html>

Vote up Vote down

Re: Redirecting a form back to the index page.

Thanks bishwadeep that helps.  Thanks falkencreative as well. big_smile Got it working, with proper code now.

Vote up Vote down

Re: Redirecting a form back to the index page.

The above resolution works with javascript but if you wanted to do it in php you could use

$page = 'index.php';
$sec = "5"
header("Refresh: $sec; url=$page");

the above code would redirect you in 5 seconds.

Vote up Vote down

Re: Redirecting a form back to the index page.

cugopob.vlad wrote:

in php you could use

$page = 'index.php';
$sec = "5"
header("Refresh: $sec; url=$page");

the above code would redirect you in 5 seconds.

Will this header function cause the new page to be refreshed every 5 seconds? If not, I like it.

Last edited by dms (August 23, 2009 5:40 pm)

Vote up Vote down

Re: Redirecting a form back to the index page.

Only if the $page variable is set to the same name as the page this code is in. That would cause a loop, reloading the same page over and over.

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down