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!
I'm new to PHP and I want to start coding the right way if possible.
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!
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
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.
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.
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.
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.
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.