1

I am trying to get my website to redirect to the home page after it submits the contact form, and goes to the thank you page. I have it working well, other than redirecting AFTER thank you page, it works for if I delete thank you page. I know I need an of statement after the line and then the header(www.google.com) type thing, but not sure what to put for the if statement.

I uploaded my contact form files into the dropbox link below, if someone could get me on the right track that would be awesome. Thanks in advance.

https://www.dropbox.com/sh/2zrci8b04989u3d/gEc3u6rPK4

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
      <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
      <title>Thank you!</title>
      <link rel="STYLESHEET" type="text/css" href="contact.css">
</head>
<body>

<script>
window.onload=function() {
  setTimeout(function() {
    location.replace("index.php");
  },3000); // wait 3 seconds
}
</script>
<h2>Thank you...</h2>
<a href="index.php">click here if you are not redirected in a few seconds</a>

</body>
</html>
Terry
  • 142
  • 4
  • 16

3 Answers3

2

Without looking at the pages since I am on my phone, put this in thank you page

<script>
window.onload=function() {
  setTimeout(function() {
    location.replace("somepage.php");
  },3000); // wait 3 seconds
}
</script>
<h2>Thank you...</h2>
<a href="somepage.php">click here if you are not redirected in a few seconds</a>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • what file would I add that to though? Contactform.php, thank-you-form.php, ? – Terry Oct 25 '13 at 03:44
  • @Terry thank-you-form.php. But javascript only works, if the browser has enabled javascript. Quite a bit companies disable javascript usage on their network computers. I would also add an html redirect in the file (even though that might not work on all browsers) – webcoder Oct 25 '13 at 03:51
  • @webcoder, I have yet to come across a company that does that. Modern browsers are rather secure, and my company has live virus checking and site blocking but would not dream of disabling javascript. I updated the answer to show a message though, just for you – mplungjan Oct 25 '13 at 03:55
  • OK so I will do that tomorrow just went into bed. so just add that in the. body part of the HTML? cool thanks I'll definitely try it and let ya know how it goes – Terry Oct 25 '13 at 04:00
  • ok so I tried it, i updated the OP with the Thank-you-Form with the code you listed, but for some reason it just directs me to a contact.php page, but only my shell shows, no contact form, so i changed the location to index.php and no luck, still loads the same contact.php shell page. – Terry Oct 25 '13 at 15:45
  • ok got it, now i just want to delay the java script from executing for a few seconds, so thank you can appear. – Terry Oct 25 '13 at 16:28
  • That is what my script does exactly. Shows thank you and redirects after 3 seconds – mplungjan Oct 25 '13 at 19:59
  • @mplungjan: Maybe a little bit late but never mind: You probably haven't had any contact to financial / rating companies or government departments. I tell can tell you, that is quite common. – webcoder Nov 07 '13 at 22:08
  • Their loss then. I assume the sites they are allowed to go to will be restricted too. – mplungjan Nov 08 '13 at 06:33
0

Try something like this, in case the user does have javascript disabled:

<?php

header( "Refresh: 5 url=http://yourdomain.com/" );

?>
<!DOCTYPE html>
<html>

<!-- HTML CODE GOES HERE -->

</html>

The number 5 in the header() function is the amount of seconds before the page redirects the user the url. The header function Must come before any html and/or php outputs

Franco Selem
  • 175
  • 1
  • 9
  • I tried it, but it would not redirect to the correct page. If you go to darthvixcustomsabers.com and go to contact, submit the form, it submits, says thank you, but then reloads a page" SOMETHING FEELS WRONG" not sure why, I have it set to redirect to google.com just to test it – Terry Oct 25 '13 at 15:58
  • ok got it, now i just want to delay the java script from executing for a few seconds, so thank you can appear. – Terry Oct 25 '13 at 16:26
0

You can do this in 3 ways.

1 - Html redirect :

<!doctype html>
<head>
<META http-equiv="refresh" content="0;URL=http://www.example.com">
</head><body></body></html>

2 - PHP Header redirect :

<?php
header('Location: http://www.example.com');
?>
<!doctype html><html><head></head><body></body></html>

3 - Javascript Redirect :

<!doctype html><html><head>
<script>
window.location.assign("http://www.example.com");
</script>
</head><body></body></html>
Srihari
  • 766
  • 1
  • 6
  • 22
  • What's with the assign? And the HTML redirect breaks the back button and makes it impossible to read the message on the page – mplungjan Oct 25 '13 at 19:58
  • @mplungjan - Nothing with the assign as such. Assign is how redirect should work without breaking back button. More info about [assign](http://stackoverflow.com/questions/4505798/difference-between-window-location-assign-and-window-location-replace). HTML Redirect doesn't break the back button as such. You can test it by double clicking back. And since there is no message in the whole page, I have provided the blank html; without any comment or unnecessary code. If you want to be able to read the message, set the timeout to your preferred seconds. It's `0` as per `content` attrib value. – Srihari Oct 26 '13 at 03:49
  • I meant this. Never used assign: http://stackoverflow.com/questions/10302905/window-location-href-property-vs-window-location-assign-method - however the refresh with 0 secs will break the back button and will not allow the visitor to read the thank you message – mplungjan Oct 26 '13 at 06:55
  • @mplungjan - refresh with 0 secs doesn't allow the user to read the message - true; breaks backward functionality - false. When you press the back button once, you go back to the previous URL and are immediately pushed forward. Browser history remains intact. Press back twice and you go the page you saw before the page where `refresh` exists. `location.replace` [breaks back functionality as it completely replaces the reference of the page you visited last](http://stackoverflow.com/questions/1865837/whats-the-difference-between-window-location-and-window-location-replace/1865840#1865840) – Srihari Oct 26 '13 at 07:47
  • When I say break, it breaks the BACK button unless you click REALLY QUICK or pull down the list. Replace may be a better choice in some cases but at least give the refresh a few secs – mplungjan Oct 26 '13 at 15:55