Ways of handling a form after its been submitted?

Associate
Joined
29 Dec 2004
Posts
2,253
I have a form, which is processed with PHP. At the end of my processing this happens:

PHP:
	if($mailto=="Nobody") {
		header('Location: http://domain.com/doku.php?id=request:generated&mailto=' . $mailto . '&urltofile=' . $urltofile);
	}
	else {
		include 'send_mail.php';
		header('Location: http://domain.com/doku.php?id=request:submitted&mailto=' . $mailto . '&urltofile=' . $urltofile);
	}

Which isn't perfect, as sometimes i get "Header already sent..." messages etc. I was wondering if there is a better way to do this?
 
The reason that you get header already sent may be that in send_mail.php there might be sending some data (even with a single echo) which means a header is automatically generated.

Not much else I can help you with. Why are you using redirects?
 
as mentioned above, check your scripts to make sure nothing is output whatsoever. any form validation/mail scripts shouldn't be echoing anything until they are complete.

Why are you using redirects?

why are you asking? don't you think it's normal? :confused:
 
as mentioned above, check your scripts to make sure nothing is output whatsoever. any form validation/mail scripts shouldn't be echoing anything until they are complete.



why are you asking? don't you think it's normal? :confused:

Hmm... am I missing something here? I have been developing web applications for a fair while and never had to use a redirect... if there was a missing file or moving the user to another part of the site...
 
Hmm... am I missing something here? I have been developing web applications for a fair while and never had to use a redirect... if there was a missing file or moving the user to another part of the site...

i thought it was quite common after a form submission. for example, i'm typing this reply on a page called newreply.php - after i post, i'm redirected back to showthread.php.....(i assume vbulletin is using redirects?? i really don't know :p)

how would you do it? :)
 
i thought it was quite common after a form submission. for example, i'm typing this reply on a page called newreply.php - after i post, i'm redirected back to showthread.php.....(i assume vbulletin is using redirects?? i really don't know :p)

how would you do it? :)

Just use a function that processes the php then use a function that continues with the page generation...
 
Just use a function that processes the php then use a function that continues with the page generation...

Sometimes helps to keep the two separate, if only so that you know certain actions are taking place in a single module behind several chained pages. i.e. SQL reads take place on output pages, inserts/updates/deletes/etc take place within the module.

In the instance above, if you've some form of global include/require, throw in a constant that will allow you to run your background module in debug mode - then when you get to the point of forking out your redirects, add another option that will echo out the redirect path (in your instance, the output you get to the client will show any extra characters that may be throwing your redirects into failure. Sometimes helps to add a couple of other print_r statements that will dump your $_SESSION, $_POST and $_GET variables so you get full feedback while in debug.
 
Just use a function that processes the php then use a function that continues with the page generation...

so you'd have post submission, validation and thread display all handled by the same script? :/

you work in the industry. i don't. but i still think that's madness. :p and won't that have adverse effects on navigation/bookmarking?

and what about setting cookies/sessions? you simply have to redirect before they take effect? well either that or display a link saying "click here to continue"??? :)
 
so you'd have post submission, validation and thread display all handled by the same script? :/

you work in the industry. i don't. but i still think that's madness. :p and won't that have adverse effects on navigation/bookmarking?

and what about setting cookies/sessions? you simply have to redirect before they take effect? well either that or display a link saying "click here to continue"??? :)

Just because I work in the industry doesn't mean I know best :) Different approaches for different tasks.

The J2EE approach is to do everything with the central controller, then redirect to a jsp page that just displays the results. If the jsp makes a connection to the database to access data, it is in addition to the main servlet that does the work - thus doing twice the work.

.NET, as far as I can tell, does things "page by page" so you have the page handle its own contents, then redirect to the next "page" to hand the next part of the process.

CMSes based on PHP use a centralised controller with parameters (task) that control incoming processing and view parameter that controls output. Work is done by a centralised system, bypassing the use of redirects.

So I guess its horses for courses.
 
Back
Top Bottom