Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

good practice will multiple forms on a page



9:21 pm on Jan 16, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

So if I have multiple forms performing different functions on the same page, is it best to put all the php code at the top of the page before I declare the doctype?

Is this considered best practice? Is this also what prevents the header already assigned errors that need to be corrected with the ob_start, etc functions?


1:20 am on Jan 17, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

The headers are sent once the script has sent any output to the user - whether that output was generated by PHP or HTML/text contained in the script. What you have to keep in mind is that even a blank line can generate output. The easiest way to prevent the issue is to begin the script with "<?php" at the very top, and
use "?><!DOCTYPE " to begin the HTML, with only PHP code in-between.

Whether or not all of your PHP code belongs above the <!DOCTYPE> is largely a matter of organization and convenience, in my opinion. Generally, you'll want to process most of the form submission data at the very beginning so you can do validation and any other necessary processing before any HTML is produced. I also tend to include local functions in this area just to make updating and debugging a little easier.

I've never been a fan of having the same file/script contain both the form and the processing code. I understand the convenience, but it's just never felt right to me, regardless of what might be considered best practice. And if you have multiple forms on the same page, I'd expect it to be easy for the code to become very convoluted and difficult to maintain. But, as I say, I'm biased.


2:17 am on Jan 18, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

So you would have the form processing script on an external file, e.g. processing.php?


7:30 am on Jan 18, 2013 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

I like to consider a .php file as one program, top to bottom fully in a <?php ... ?> and output html as needed where needed without any naked html at all.
But it's personal preference I guess.

Featured Threads

Hot Threads This Week

Hot Threads This Month