tags:

views:

227

answers:

7

I have a one page site that has PHP code in it. Once the user presses 'Send', this sends the information to my email, then displays a messagebox saying that the action was a success to the user - great.

After the messagebox is closed, the website stays at website.com/report.php. Is there a way to redirect it back to the original page.

Also, any way to change the icon in the messagebox that pops up? Here is the code that I have: <script language="JavaScript">alert("Your request has been sent. I will contact you soon!");</script>

Thanks.

+4  A: 

Look into window.open and window.location

Place it after your alert()

http://www.tizag.com/javascriptT/javascriptredirect.php

Also, to answer your messagebox icon question: No, it is browser-dependent and not modifiable.

If you want to do that, your are going to need to fake it with html/css and javascript.

Dan McGrath
Make sure you degrade gracefully for those without javascript. You don't want a non JS enabled user to get stuck on a blank page :)
BStruthers
While I'd avoid the javascript altogether, your the only one (so far) to not suggest used a header location when he's asking how to redirect after sending the page contents.
Tim Lytle
and to cause an automatic redirect using a timed delay without using javascript check out the meta tag for http-equiv="refresh" <http://www.tipsntutorials.com/tips/HTML/42>
Dan McGrath
A: 

Use the php header command

<?php
header("Location: http://www.example.com/"); 
exit;
?>
Will Shaver
Nice, you beat me to it by 15 seconds.
Frank DeRosa
This method would not display the alert.
Tim Lytle
A: 

Towards the end of your php, use this:

header('location: home.php');

This will cause the browser to load the original page.

I don't believe that the standard alert box can be altered, aside from the message. You can't change the title or the buttons, either.

Frank

Frank DeRosa
This method would either not display the alert, or not actually redirect. Either the header would be sent before the page content (and the alert JS) causing the page to be redirected without an alert, or if the page contents has been sent before the header is set, the header won't have any effect.
Tim Lytle
Ahh, I made the assumption that the first page triggered the alert, not the processing script. I suppose he could buffer the output... but that sounds like a lot more trouble than just using `window.location()` after `alert()`.
Frank DeRosa
A: 

To do a redirect in PHP, use header("Location: page.php"); for this. Before and after this your code shouldn't be sending any other output to the response. Eventually use exit(); to terminate the script afterwards.

If you need the page which was requested right before this page, then best what you can do is to include its URL as request parameter of the link to report.php and use it as redirect destination. E.g.

<a href="report.php?referrer=<?php echo $_SERVER['REQUEST_URI']; ?>">report</a>

and in the report.php pass it as hidden input field:

<input type="hidden" name="referrer" value="<?php echo getParam("referrer"); ?>">

And after submitting the report do:

header("Location: " . getParam("referrer") . ")"; // getParam() returns sanitized GET parameter.

An alternative is to use the $_SERVER['HTTP_REFERER'] header (yes, including the typo) for this, but this is just not that reliable as it may be disabled or spoofed by the client.

BalusC
+1  A: 
    <script language="JavaScript">
alert("Your request has been sent. I will contact you soon!");
window.location.assign("http://website.com");
</script>

If you want to change the icon in alert box or make it look a little fancy, you could try YUI dialog

vsr
A: 

I believe a more elegant solution is to simply present the user with a confirmation page (instead of an alert box), and place a link to the previous page there.

That would at lease work for all users.

For those users with javascript a little Ajax (jQuery) could submit the form for you, and display the confirmation nessage. All without leaving the page the user is on (negating the need for any fancy redirects).

Tim Lytle
A: 

Use this code to display the alert:

function displayAlert(message, redirect) {
    alert(message);
    window.location.href = redirect;
}

Then, you can use code like:

displayAlert("This is the message", "http://redirect.the/user/here");
mattbasta