/    Sign up×
Community /Pin to ProfileBookmark

How do i?.. Add a msg box alert before changing header.

Hi i have a php mailer which i use with a html email form and this php file has

header(“Location: http://google.com/“);

to redirect it to google at the end.

I would like to somehow add a popup that says “Thank you for your message” before changing the page i managed to get a popup to work by echoing a javascript alert but this gives the redirect a error about already setting headers.

Has anyone got a suggestion on what i should do?

(if this is in the wrong section im sorry i thought it would be best in php since its in a php file but if you feel it should be in client side somewhere please move it)

If anyone can help i would be very grateful.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@opifexOct 07.2009 — Just take out the offending code!

or change it to
[code=php]header("Location: http://mysite.com/thankyou.html");[/code]
that is nicely designed to match the rest of your site.

It's a little hard to tell you much more without seeing the rest of the code...

...a 3TB HDD??? SATA or RAID ?
Copy linkTweet thisAlerts:
@lilsammyauthorOct 07.2009 — Well i want to keep the header to redirect to the index page and a popup to say "Thank you for your email blah blah"

Any ideas?

3TB Total Space, 1x1TB 2x500GB 4x250GB I had them setup in a raid but i found i didn't need the benifits of a raid anymore so i triple boot Ubuntu, Windows XP SP3, Windows XP x64.

EDIT:

Full php file.

[CODE]
<?PHP


###################### Set up the following variables ######################
#
$to = "[email protected]"; #set address to send form to
$subject = "Request from Free Demo contact form"; #set the subject line
$headers = "From: [email protected]"; #set the from address, or any other headers
$forward = 1; # redirect? 1 : yes || 0 : no
//$location = "index.html"; #set page to redirect to, if 1 is above
#
##################### No need to edit below this line ######################

## set up the time ##

$date = date ("l, F jS, Y");
$time = date ("h:i A");

## mail the message ##

$msg = "Below is the result of your feedback form. It was submitted on $date at $time.nn";

if ($_SERVER['REQUEST_METHOD'] == "POST") {
foreach ($_POST as $key => $value) {
$msg .= ucfirst ($key) ." : ". $value . "n";
}
}
else {
foreach ($_GET as $key => $value) {
$msg .= ucfirst ($key) ." : ". $value . "n";
}
}

mail($to, $subject, $msg, $headers);
if ($forward == 1) {

header ("Location:$location");

}
else {
echo "Thank you for submitting our form. We will get back to you as soon as possible.";
}

?>
[/CODE]
Copy linkTweet thisAlerts:
@MindzaiOct 07.2009 — Popups are created on the client side, and the header redirection takes place on the server side. You can't do both together like that. Why not just use javascript to do the redirection and the popup?
Copy linkTweet thisAlerts:
@lilsammyauthorOct 07.2009 — Well i dont have enough knowledge to do it correctly.

Currently JavaScript validates the form on a html page and if its ok it sends it through php then the php redirects to the index page. I tried to change the javascript so that if there is no error redirect the page but for some reason it breaks the validation so i tried to do it in php instead.

The JavaScript i have for validation is here:

http://www.webdeveloper.com/forum/showthread.php?p=1038352

I dont see the point in quoting it here.
Copy linkTweet thisAlerts:
@MindzaiOct 07.2009 — Redirecting after validating with javascript is not going to work because the data will never be posted. Without delving into AJAX, your options are to send the popup after the javascript validation and then let PHP do the redirect, or else output some javascript from your PHP script which shows the popup and then redirects.
Copy linkTweet thisAlerts:
@opifexOct 07.2009 — ok.

since you have the option to redirect OR show a thank you message already....

replace this
[code=php]header ("Location:$location");[/code]
with this
[code=php]
echo "<script type="text/javascript">n";
echo "alert('Thank you for submitting our form.');n");
echo "window.location = ('$location');n";
echo "</script>";[/code]
Copy linkTweet thisAlerts:
@lilsammyauthorOct 07.2009 — Ok thanks for the help but i thought about it and instead what i did was make the link to the contact us page open a new window then after the form was submited i made it redirect the window to a page with thank you on it and a close window button so i kinda took a easy way out. Thanks for your input tho guys.
Copy linkTweet thisAlerts:
@SyCoOct 07.2009 — Opening a new window is a bit of a pain for users as the can accidentally blur it behind the main window and unless you have focus set onclick it sits behind. It's one reason why most sites have moved away from using them.

I've recently started using the jQuery dialog box. Basically a div that you can show/hide, fill with content using ajax or javascript, drag and resize it, style it and use it just like IE's modal window but cross browser. All the benefits of a popup and none of the drawbacks.
Copy linkTweet thisAlerts:
@MindzaiOct 07.2009 — +1 for the jQuery UI tools.
×

Success!

Help @lilsammy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...