General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 14th, 2003, 12:23 PM
RJDavison RJDavison is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 11 RJDavison User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Form submit redirection

I know this subject has been raised a few times on here, its just that im not having much joy with any of the answers.

I have the below code to do a simple mail function. Im trying to get the form to redirect once the submit button has been pressed.

Ive tried the 'header'function in the position as below and its bringin up errors.

Any suggestions would stop me pulling out my hair.



<?php

if ($submit) { // If they've submitted the form...

if ($customerName <= "")
$message = "Please enter your name.";
else if ($customerEmail <= "")
$message = "Please enter your email address.";
else if ($customerMessage <= "")
$message = "Please insert your message.";

if ($message) // If we've set a message with a warning about their form entry...
echo ($message);
else {
mail("webmaster@turkeybrain.co.uk",
"Form Results",
"$customerName submitted the following message

Subject: $subject
Operating System: $operatingsystem
Browser: $browser
Message: $customerMessage",
"From: $customerName <$customerEmail>");

echo ("Thank you for submitting your message. A representative will contact you shortly if necessary.");
header("Location:http://www.turkeybrain.co.uk");
die(); // We can stop if the form's been submitted and we've thanked them.
} // end if we're sending the email

} // end if they hit "Submit"

?>

Reply With Quote
  #2  
Old December 14th, 2003, 03:01 PM
hagur hagur is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Iceland
Posts: 11 hagur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
You cannot use the Header function when you've already sent output to the browser.

Header(); must be called before anything else on the page.

Instead of displaying the thank you message on this page, redirect to another page which displays the message, and uses metarefresh to redirect back to turkeybrain.co.uk in a few seconds - you must give people a chance to read the message

Reply With Quote
  #3  
Old December 14th, 2003, 03:12 PM
RJDavison RJDavison is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 11 RJDavison User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the assistance.

Ive been reading more on the subject and now understand the concept of headers. Duh !!!

If not using the headers command, what other methods within php are available to redirect to the new page ?

Reply With Quote
  #4  
Old December 14th, 2003, 08:32 PM
Mike_r Mike_r is offline
ExoCrew
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 68 Mike_r User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 15
You can try the <meta tag of HTML.

echo "<meta http-equiv=\"Refresh\" content=\"1;URL=http://url.com/file.html\">";

Reply With Quote
  #5  
Old December 15th, 2003, 08:23 AM
RJDavison RJDavison is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 11 RJDavison User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Meta tag redirection

Thanks for the suggestion, although this has thrown up its own problems. By using this meta tag I can go into my form page, but then it just automaticly redirects after the set period. I just need it to redirect after I press submit.

I know this isnt the right forum for java script but I have been experimenting using the following:

<BR />
<INPUT TYPE = "submit" onblur = "doVerify()" NAME = "submit" VALUE = "Submit Form"/>
<BR />
<script language="JavaScript">
function doVerify()
{
location.href = "http://www.turkeybrain.co.uk/thanks.htm"
}
</script>

I've assigned an action upon the submit of the button. Now this works nicely on my apple mac running OSX 10.3, but wont redirect on my PC running XP. The screen just goes blank.

Can anyone shead any light on this of help with the original problem.

Reply With Quote
  #6  
Old December 15th, 2003, 09:33 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to dhouston
I believe what Mike_r was proposing was that on the PHP page wherein you perform the mail function, you print the meta refresh as the only output so that it then redirects over to the thanks page.

Why would you not want to use the header() function in PHP? It's the standard way of redirecting.

Reply With Quote
  #7  
Old December 15th, 2003, 09:45 AM
RJDavison RJDavison is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 11 RJDavison User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
PHP redirection

I've attempted to use the header() command, but get an output error saying that the headers have already been sent.

Not understanding PHP in much depth im not knowledgeable enough to try and debug this error.

Reply With Quote
  #8  
Old December 15th, 2003, 09:51 AM
hagur hagur is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Iceland
Posts: 11 hagur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Okey, let me see if I understand you correctly.

You have a page, on which is a form.

You submit the form to this PHP script:

<?php

if ($submit) { // If they've submitted the form...

if ($customerName <= "")
$message = "Please enter your name.";
else if ($customerEmail <= "")
$message = "Please enter your email address.";
else if ($customerMessage <= "")
$message = "Please insert your message.";

if ($message) // If we've set a message with a warning about their form entry...
echo ($message);
else {
mail("webmaster@turkeybrain.co.uk",
"Form Results",
"$customerName submitted the following message

Subject: $subject
Operating System: $operatingsystem
Browser: $browser
Message: $customerMessage",
"From: $customerName <$customerEmail>");

echo ("Thank you for submitting your message. A representative will contact you shortly if necessary.");
header("Location:http://www.turkeybrain.co.uk");
die(); // We can stop if the form's been submitted and we've thanked them.
} // end if we're sending the email

} // end if they hit "Submit"

?>

This PHP-script submits the information from the form via email. You want to display a message to the user, telling him that the message was sent. Then, you want to redirect the user back to http://www.turkeybrain.co.uk

Okay, like you've stumbled upon, you cannot call the echo function, then the Header function.

This is really simple. The only thing the PHP script should to is send the e-mail, THEN redirect to a specific thank you page which displays the message.

On this thank-you page you can display a link below the message, when clicked, redirects the user to http://www.turkeybrain.co.uk. Furthermore, this thank-you page can contain a <META refresh> command to automatically send the user back to http://www.turkeybrain.co.uk in a given period of time, if he doesn't click the link himself.

Reply With Quote
  #9  
Old December 15th, 2003, 09:59 AM
RJDavison RJDavison is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 11 RJDavison User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
PHP redirection

So, let me get this right. delete the echo commands. Insert the header() command to my thank you page, and bob should be by uncle as they say over here?

Reply With Quote
  #10  
Old December 15th, 2003, 10:00 AM
hagur hagur is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Iceland
Posts: 11 hagur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
In even more detail

Modify the PHP script so that it looks like this:

<?php

if ($submit) { // If they've submitted the form...

if ($customerName <= "")
$message = "Please enter your name.";
else if ($customerEmail <= "")
$message = "Please enter your email address.";
else if ($customerMessage <= "")
$message = "Please insert your message.";

if ($message)
//An error occured, redirect to the error.php file which displays the message to the user
Header("Location: error.php?msg=" . $message);
else {
mail("webmaster@turkeybrain.co.uk",
"Form Results",
"$customerName submitted the following message

Subject: $subject
Operating System: $operatingsystem
Browser: $browser
Message: $customerMessage",
"From: $customerName <$customerEmail>");
// No input errors occured, the e-mail has been sent, let's redirect the user to the thank you page.
header("Location:thankyou.html");
} // end if we're sending the email

} // end if they hit "Submit"

?>

Now, create the file thankyou.html and place it in the same directory as the PHP script. (Or modify the header function call accordingly)

The thankyou.html file should contain the message to display to the user, and a link to send him back to turkeybrain.co.uk (Or even a meta refresh)

Note that I also modified the part of the script that validates the input. Instead of echo-ing out the error, I redirect the user to another php script called error.php. You mustn't call echo at all from the PHP script, since then you won't be able to call the Header function.

the error.php file takes the message as a parameter through the query-string. The contents of the error.php file could be something like this:

<html>
<head>
<title>Error!</title>
</head>
<body>
<?echo $_GET["msg"]?>
<p>
<a href="#" onClick="javascript:history.go(-1);>Go back!</a>
</p>
</body>
</html>

In other words, move all the "echo-functionality" to other files, and simply redirect to them accordingly.

I hope this helps

Reply With Quote
  #11  
Old December 15th, 2003, 10:06 AM
RJDavison RJDavison is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 11 RJDavison User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile PHP Redirection.

Youre a Gent. I will give this a try this evening.

Apologies for being so dumb. Guess we all have to start somewhere.http://forums.devarticles.com/#

Reply With Quote
  #12  
Old December 15th, 2003, 10:09 AM
hagur hagur is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Iceland
Posts: 11 hagur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post

No problem

Remember that you won't learn anything unless you ask dumb questions

Reply With Quote
  #13  
Old December 15th, 2003, 03:28 PM
Mike_r Mike_r is offline
ExoCrew
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 68 Mike_r User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 15
Well, you must have used <meta in the wrong place, I knew the header() one is the best solution but I was just pointing out some other possibilities, <meta must have been used in current place of header("Location: ); ..

Reply With Quote
  #14  
Old December 16th, 2003, 03:29 AM
RJDavison RJDavison is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 11 RJDavison User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
PHP Form Redirection

When using the scrip above which hagur kindly changed im getting the following message:

Warning: Cannot add header information - headers already sent by (output started at /home/httpd/vhosts/turkeybrain.co.uk/httpdocs/form.php:6) in /home/httpd/vhosts/turkeybrain.co.uk/httpdocs/form.php on line 44

Line 44 of the code is the header() line which redirects to my thankyou.html file.

As im learning with each step, can anyone point me in the right direction as to why this is happening.

Im aware that you can call the header() command if you have called the echo function, but these have now all gone and been replaced by an independant error.php file which echo's the error output for me.

Reply With Quote
  #15  
Old December 16th, 2003, 05:39 AM
hagur hagur is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Iceland
Posts: 11 hagur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi,

If you are getting this error: (Cannot add header information - headers already sent ...)

That means that some output was already sent to the browser before you called the Header() function.

At first glance, I can't see any output being produced at all in the PHP-script, but that doesn't mean there isn't any.

Is it possible that the mail() function is returning an error message?

What you need to to is "debug" the script. The easiest way to do that, in this case, is to comment out both of the Header() function calls and try submitting the form.

You should get a blank page, since nothing is being echo-ed and there's not redirection. It's likely that some error message is being returned or something unexpected like that, therefore causing the Header() function to fail.

Try doing this, then tell us what you get.

Reply With Quote
  #16  
Old December 16th, 2003, 05:45 AM
RJDavison RJDavison is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 11 RJDavison User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
PHP Form Redirection

If you would prefer, you can see for yourself what the form is doing and the error it is outputting. Use the following link:

www.turkeybrain.co.uk/form.php

Reply With Quote
  #17  
Old December 16th, 2003, 06:39 AM
hagur hagur is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Iceland
Posts: 11 hagur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Okay,

Form.php is the actual form. Is this code also from this file?

<?php

if ($submit) { // If they've submitted the form...

if ($customerName <= "")
$message = "Please enter your name.";
else if ($customerEmail <= "")
$message = "Please enter your email address.";
else if ($customerMessage <= "")
$message = "Please insert your message.";

if ($message)
//An error occured, redirect to the error.php file which displays the message to the user
Header("Location: error.php?msg=" . $message);
else {
mail("webmaster@turkeybrain.co.uk",
"Form Results",
"$customerName submitted the following message

Subject: $subject
Operating System: $operatingsystem
Browser: $browser
Message: $customerMessage",
"From: $customerName <$customerEmail>");
// No input errors occured, the e-mail has been sent, let's redirect the user to the thank you page.
header("Location:thankyou.html");
} // end if we're sending the email

} // end if they hit "Submit"

?>


If that's the case, you must move that code to the top of the file, since you cannot write the form itself to the browser, and then try a Header() redirect.

The same applies to writing out regular HTML content as calling echo, Therefore you must do this check and redirect accordingly before you write out the HTML form.

You could also separate these two files aswell.

Then you'd have form.php which includes the form, as it is now at
turkeybrain.co.uk/form.php (Could aswell be form.html since no PHP functionality is required there.)

Then create a nother file called form_send.php which includes this code:

<?php

if ($submit) { // If they've submitted the form...

if ($customerName <= "")
$message = "Please enter your name.";
else if ($customerEmail <= "")
$message = "Please enter your email address.";
else if ($customerMessage <= "")
$message = "Please insert your message.";

if ($message)
//An error occured, redirect to the error.php file which displays the message to the user
Header("Location: error.php?msg=" . $message);
else {
mail("webmaster@turkeybrain.co.uk",
"Form Results",
"$customerName submitted the following message

Subject: $subject
Operating System: $operatingsystem
Browser: $browser
Message: $customerMessage",
"From: $customerName <$customerEmail>");
// No input errors occured, the e-mail has been sent, let's redirect the user to the thank you page.
header("Location:thankyou.html");
} // end if we're sending the email

} // end if they hit "Submit"

?>

Then modify the form action so it points fo form_send.php

Reply With Quote
  #18  
Old December 16th, 2003, 06:50 AM
RJDavison RJDavison is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 11 RJDavison User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
PHP Form Redirection

Blimey !! If i knew it was going to get this involved I should have just put a snail mail address on my website for them to send their comments too !!!

Kidding !!

Thanks for the help and I will give your suggestions a go.

Reply With Quote
  #19  
Old December 19th, 2003, 05:05 PM
jfmckenna jfmckenna is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Blacksburg VA USA
Posts: 1 jfmckenna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
try putting: ob_start();
on the very first line for output buffering

Reply With Quote
  #20  
Old December 21st, 2003, 01:00 PM
mattp23 mattp23 is offline
Moderated
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: UK
Posts: 82 mattp23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 43 m 44 sec
Reputation Power: 15
also make sure there are no spaces or characters before your <? in form_send.php

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Form submit redirection


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap