Jump to content

Archived

This topic is now archived and is closed to further replies.

jwk811

Redirecting!

Recommended Posts

I have a form that leads to a script to be emailed. When the person submits the form and goes to the script page I want to have them redirected to another page and I know it's possible but I just can't figure it out. So basically how do I make a page go to another page once someone views it?

Share this post


Link to post
Share on other sites
[code=php:0]
header("Location : newpage.php");
[/code]

Be aware however thast [b]no[/b] output can be sent to the browser before calling the header function.

Share this post


Link to post
Share on other sites
do you mean if i have the redirect code in the script then i can't send the email? and where would i put the line?

Share this post


Link to post
Share on other sites
isnt there a way i could do the if..else stament if it was mailed have it go to a url and if it wasnt have to say something?

Share this post


Link to post
Share on other sites
[quote author=jwk811 link=topic=110120.msg444547#msg444547 date=1159676415]
do you mean if i have the redirect code in the script then i can't send the email? and where would i put the line?
[/quote]
no you can do a mail function. you just can't have any html output before a header. example:

mail(...);
header(...);

is fine.

mail(...);
echo "blah";
header(..);

is not.

Share this post


Link to post
Share on other sites
or if you want to put output simply put this at the top of your code then at the bottom:

[code]
ob_start();

<---
CODE
--->

ob_end_flush();
[/code]

Share this post


Link to post
Share on other sites
lol. soz i didnt c that. and with mine they dont have to go to a site. :D i'll look next time. sorry.

Share this post


Link to post
Share on other sites
Okay thanks. In this code.
[code]if(mail(...)) header("Location: http:///www.domain.com");
else {
//something
}[/code]
I would put
[code]if (mail($to;$message;$subject;$header)) header ('Location: http://www.redirectpage.com')
else {
echo "there was a problem"
}[/code]
is that how i should do it?

Share this post


Link to post
Share on other sites
Yeah, but it would have to be [code]mail($to,$message,$subject,$header)[/code] since you seperate arguments with commas.

Share this post


Link to post
Share on other sites
okay, im getting a few problems...
this is my code that should send an email and redirect to another page
[code]<?php
ob_start();
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$to = "(my-email)";
$subject = "Selling Chips";
$message = "<b>$name</b>'s comment.---$comment.";
$headers = "From: $email";
mail($to,$subject,$message,$headers);
if (mail($to,$subject,$message,$headers)) header("Location: thankyou.php");
else "echo A problem occured";
ob_end_flush();
?>[/code]
is that right? because i get this page coming up.. its the page that the form was directed to with a warning at the top...
[code]Warning: Cannot modify header information - headers already sent by (output started at /home/content/j/w/k/jwk811/html/process.php:5) in /home/content/j/w/k/jwk811/html/process.php on line 37
[/code]
And actually get sent two emails so the if (mail...) is actually sending me another.. whats the problem? cant figure it out

Share this post


Link to post
Share on other sites
okay that script in and of itself does not need the ob_start/ob_end_flush functions.  Is this script a code block in a larger script, or is it being included in another script? If so, again, there cannot be any html output before the header.  you will have to extend your ob stuff to include the other stuff.

If this script is in its own file and it's not being included into something else, then check to make sure you don't even have a blank line before your <?php tag, cuz that counts as html output

Share this post


Link to post
Share on other sites
Thank you very much. But just one more thing.. the if(mail..) is sending me another email.. i got the redirect to work now but what about the email and if something did go wrong would the else stament say the error if the if thing isnt going right?

Share this post


Link to post
Share on other sites
you are getting 2 emails because you are sending 2 emails.

[code]
mail($to,$subject,$message,$headers); // take out this line
if (mail($to,$subject,$message,$headers)) header("Location: thankyou.php");
else echo "A problem occured";
[/code]

you need to take one of them out. i suppose you should take out the first one, so as to leave in some error handling. Also on your else statement, move the " you have your quotes wrapped around echo.  (I changed it in the code I listed, for reference).

Share this post


Link to post
Share on other sites
okay thanks ive got it now. how would i be able to make it so it won't redirect if they forgot to type in there name, email, etc?

Share this post


Link to post
Share on other sites
you need to setup some error checking for your form. wrap that thing inside some more conditioning.

VERY basic example:
[code]
if (!$_POST['name'] or !$_POST['email']) {
  echo "please fill out your form";
else {
  if (mail($to,$subject,$message,$headers)) header("Location: thankyou.php");
  else echo "A problem occured";
}
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.