jwk811 Posted October 1, 2006 Share Posted October 1, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/ Share on other sites More sharing options...
trq Posted October 1, 2006 Share Posted October 1, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101679 Share on other sites More sharing options...
jwk811 Posted October 1, 2006 Author Share Posted October 1, 2006 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 Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101680 Share on other sites More sharing options...
jwk811 Posted October 1, 2006 Author Share Posted October 1, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101683 Share on other sites More sharing options...
extrovertive Posted October 1, 2006 Share Posted October 1, 2006 if(mail(...)) header("Location: http:///www.domain.com");else {//something} Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101687 Share on other sites More sharing options...
.josh Posted October 1, 2006 Share Posted October 1, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101708 Share on other sites More sharing options...
Daniel0 Posted October 1, 2006 Share Posted October 1, 2006 Output control could fix that problem:http://php.net/outcontrol Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101738 Share on other sites More sharing options...
JasonLewis Posted October 1, 2006 Share Posted October 1, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101751 Share on other sites More sharing options...
Daniel0 Posted October 1, 2006 Share Posted October 1, 2006 I just said that... Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101752 Share on other sites More sharing options...
JasonLewis Posted October 1, 2006 Share Posted October 1, 2006 lol. soz i didnt c that. and with mine they dont have to go to a site. :D i'll look next time. sorry. Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101755 Share on other sites More sharing options...
jwk811 Posted October 1, 2006 Author Share Posted October 1, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101826 Share on other sites More sharing options...
Daniel0 Posted October 1, 2006 Share Posted October 1, 2006 Yeah, but it would have to be [code]mail($to,$message,$subject,$header)[/code] since you seperate arguments with commas. Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101829 Share on other sites More sharing options...
jwk811 Posted October 1, 2006 Author Share Posted October 1, 2006 okay, im getting a few problems...this is my code that should send an email and redirect to another page[code]<?phpob_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 Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101846 Share on other sites More sharing options...
.josh Posted October 1, 2006 Share Posted October 1, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101856 Share on other sites More sharing options...
jwk811 Posted October 1, 2006 Author Share Posted October 1, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101863 Share on other sites More sharing options...
.josh Posted October 1, 2006 Share Posted October 1, 2006 you are getting 2 emails because you are sending 2 emails. [code]mail($to,$subject,$message,$headers); // take out this lineif (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). Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101866 Share on other sites More sharing options...
jwk811 Posted October 1, 2006 Author Share Posted October 1, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101872 Share on other sites More sharing options...
.josh Posted October 1, 2006 Share Posted October 1, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/22634-redirecting/#findComment-101876 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.