Jump to content


Photo

good practice with multiple forms on a page


  • Please log in to reply
7 replies to this topic

#1 1internet

1internet

    Advanced Member

  • Members
  • PipPipPip
  • 137 posts

Posted 16 January 2013 - 05:36 PM

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? Oppossed to the php code above each form?

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?


#2 RussellReal

RussellReal

    Advanced Member

  • Members
  • PipPipPip
  • 1,774 posts
  • LocationTobyhanna, Pennsylvania

Posted 16 January 2013 - 05:54 PM

Well, I'll be quite fair, if you're going to be submitting multiple forms or just 1 form, you're best running the processing code in a separate file "process.php" or similar.

The reason I say this, is because if you use the same page as the recipient, its very hard for you to avoid accidental form resubmission (hitting the refresh button)

You could also use the same page as the processing page (If you absolutely MUST) aslong as you redirect the user to a "confirmation" page, and get them away from that page AFAP (sounds dirty huh!)

But to answer your question, yes -- running your form processing code before ANY character data (even spaces and new lines) will avoid the "headers already sent" error, but if you echo, print, etc, information to the browser before you call header() you will receive that error anyway.

- Russell
Like my help and want to hire me? Email me or add me to MSN at RussellonMSN@hotmail.com

#3 1internet

1internet

    Advanced Member

  • Members
  • PipPipPip
  • 137 posts

Posted 16 January 2013 - 06:00 PM

Thanks Russel, what if I don't use the same page as the recipient, or when I do, the form is no longer shown?

#4 RussellReal

RussellReal

    Advanced Member

  • Members
  • PipPipPip
  • 1,774 posts
  • LocationTobyhanna, Pennsylvania

Posted 16 January 2013 - 06:08 PM

Nope, usually on Error you will forward the user back to the page with the form like: header('Location: index.php?err=1');
Like my help and want to hire me? Email me or add me to MSN at RussellonMSN@hotmail.com

#5 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 16 January 2013 - 06:15 PM

It's not hard to avoid the refresh problem - when done processing, use header to send them back to the same page. Just like if you use a separate page. ...
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#6 RussellReal

RussellReal

    Advanced Member

  • Members
  • PipPipPip
  • 1,774 posts
  • LocationTobyhanna, Pennsylvania

Posted 16 January 2013 - 06:33 PM

Yes, but the only reason people will use the same page to process form data is if they're trying to use the form data collected in targeting erroneous fields. (For example: Long Profile Detail Pages -- which should use AJAX of some sort to be quite fair)

While that is a decent approach, for that application, in almost every other application its best to process on separate pages, atleast in my professional experience.

- Russell
Like my help and want to hire me? Email me or add me to MSN at RussellonMSN@hotmail.com

#7 1internet

1internet

    Advanced Member

  • Members
  • PipPipPip
  • 137 posts

Posted 16 January 2013 - 06:45 PM

I currently dont use a redirect for the errors, if there are no errors it redirects to another page, but if there errors it will remain on the same page from the form action="", with the $errors loaded in the array and displayed to the user.
Is this approach acceptable?

#8 RussellReal

RussellReal

    Advanced Member

  • Members
  • PipPipPip
  • 1,774 posts
  • LocationTobyhanna, Pennsylvania

Posted 16 January 2013 - 06:59 PM

Yes, that approach is what Jessica outlined in her post and I outlined in my follow-up post.

- Russell
Like my help and want to hire me? Email me or add me to MSN at RussellonMSN@hotmail.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com