homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

good practice will multiple forms on a page

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

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)

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)

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)

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved