Redirecting page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
westen
Forum Newbie
Posts: 14
Joined: Mon Oct 29, 2007 9:03 am

Redirecting page

Post by westen »

I have an html form that a user fills in. When the user submits the form the input is checked and if there there are any errors they are displayed. This works fine. If there are not any errors I wish to redirect to another page. The only function I can find for redirecting is header() but I do not understand how it works. It says I am not allowed to display anything before calling it but I clearly have to display my form before redirecting to another page. Can someone please explain to me how this works, I would appreciate any help.

Apologies if this is a repost, I attempted to post this question on friday but I lost my net connection and I cannot find the thread so I think it did not post properly.

Cheers,
westen.
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Form Action

Post by N1gel »

You don't want to use the header for submitting a form.

You want to set the action of the form to the page where you are going to process the form. like

Code: Select all

<FORM action="http://somesite.com/prog/adduser" method="post">
you could try looking @

w3 on forms

or just search google for 'form action' or 'form post'

its prety simple you just set the action of the form to be the page you want to send them to
westen
Forum Newbie
Posts: 14
Joined: Mon Oct 29, 2007 9:03 am

Post by westen »

Yes I submit the form and check the input using the same php file, if the input is without errors I then want to redirect to another page, if it has errors the same page is displayed with the errors written at the top. The form submission and error checking works I just do not know how to redirect.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Code: Select all

header('Location: http://yourdomain.com/the_page_for_no_errors.php');
die();//don't forget to die after you send the header!
that will do the trick
westen
Forum Newbie
Posts: 14
Joined: Mon Oct 29, 2007 9:03 am

Post by westen »

Thanks, but thiat is what I already had.
I get the following error message:

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/ulondoco/public_html/header.php:5) in /home/ulondoco/public_html/register.php on line 198
Cheers guys.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That particular error is posted about a LOT. Please search for the solution.
westen
Forum Newbie
Posts: 14
Joined: Mon Oct 29, 2007 9:03 am

Post by westen »

Apologies and thanks. I am sorted now, I followed someones advice to call ob_start() and ob_end_flush(), I have no idea what this does but it works!

Cheers,
Westen
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Unfortunately, anyone who suggests using output buffering as a solution is generally delusional. i.e. It is not a solution. It's a work-around that could easily be avoided if the code was written such that all page logic happens before any output starts.
westen
Forum Newbie
Posts: 14
Joined: Mon Oct 29, 2007 9:03 am

Post by westen »

Really? I was not aware that it was bad form to mix in the output with the logic, so you save everything during the logic stage in variables and then ouput it all at the end?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Separating business/program logic from presentation logic is the logical conclusion when one wishes to keep both fairly easy to maintain. This is especially true in environments where the programmer doesn't create the page designs (and pages.)
Post Reply