submitting forms to themselves

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
benedict5980
Forum Newbie
Posts: 6
Joined: Sun Aug 24, 2003 10:52 am

submitting forms to themselves

Post by benedict5980 »

Hi everyone,

I'm new to PHP and I want to start coding the right way if possible. :D

I've seen codes wherein a form is submitted to itself('PHP_SELF'). I was wondering about which is better: submitting a form to itself or submitting to another file?

Your insights on this will be most appreciated. Thanks in advance! :D
RFairey
Forum Commoner
Posts: 52
Joined: Fri Jun 06, 2003 5:23 pm

Post by RFairey »

Self submission means all your code is in one file, instead of in two, the HTML form and the handling code.
benedict5980
Forum Newbie
Posts: 6
Joined: Sun Aug 24, 2003 10:52 am

Post by benedict5980 »

Exactly RFairey! :D

I want know which is better? Is one method better than the other? :D
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if there's no framework telling me otherwise I like to keep the code of view(s) and controller of a component as near together as possible. But that's still open to debate ;)
User avatar
brewmiser
Forum Commoner
Posts: 74
Joined: Mon Aug 18, 2003 12:50 pm
Location: Dallas, TEXAS

Post by brewmiser »

I would think that it would have to do with your preference. It would seem that you have a choice of dealing with only a couple of large files with everything in it, or breaking it up into smaller files, but having to remember which file it was in.
benedict5980
Forum Newbie
Posts: 6
Joined: Sun Aug 24, 2003 10:52 am

Post by benedict5980 »

But performance wise? Is there a difference between the two? Which is faster?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Neither is really faster. The act of submitting the form is the same, and the speed is the same. The fact that you have both form display and form handling stuff in the same file "could possible maybe" have a chance of slowing the script down every so slightly at the smallest level depending on how you structure your code.

So the answer is No. It won't slow anything down. Using the MVC style is pretty much going to set up the fact that you will use the same page anyways, so all is on your side.
benedict5980
Forum Newbie
Posts: 6
Joined: Sun Aug 24, 2003 10:52 am

Post by benedict5980 »

I see now. Thanks alot! I thought one was better than the other. Thanks again! :D
BassPlayer
Forum Newbie
Posts: 12
Joined: Sun Aug 24, 2003 10:18 pm

Post by BassPlayer »

I use this alot and it's really nice. I also found and idea on the net of using a hidden form element called op. So say I have a script called admin.php which has a bunch o if/elseif statements looking for particular op values.. When admin.php is first run op is "" so it gives the main menu. Once I select something like edit user op becomes edituser and admin.php being the form action is called again. Admin.php walks its if statements and when it gets to edit user it prints the edit user form which has admin.php as the form action. etc etc you get the picture.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

which is better? that's hard. personally i prefer one page if you're doing error handling.

easier to repopulate the form since you have one function that does all the display and another that prepends error messages (if any) and a success.. just have a hidden element that tells it whether it's a submission or not.

if you want to see a rather complicated example.... http://24.91.157.113/findyourdesire/join.showcode
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

I personally prefer to use different pages. I'll have web viewable pages in one directory and processing scripts in another. Yes it means I have to do a little more work to pass values around to pre-populate forms/error messages, however it keeps both files shorter and simpler.

The web displayable files end up just being a series of calls to either my database layer to get content or to my html layer to format it.

The script files use mainly generic utility functions and regexps to validate and then the database layer to transparently handle inserts/updates/delete before passing control back to another vieable file.

At times I'll use the self-submitting page, but I won't use the mutliple forms with processing all in one page paradigm. It is just too ugly in my opinion. Anytime I see a whole application done in one page with switch/if else if... I cringe. That doesn't mean its wrong, others have the same reaction to my design.
Post Reply