Form Submissions ... where to and error handeling

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
sixes
Forum Newbie
Posts: 2
Joined: Sat Jan 24, 2009 7:12 pm
Location: Knoxville

Form Submissions ... where to and error handeling

Post by sixes »

Hello all. My question has to do with how you handle form submissions and the subsequent errors.

Do you post the form to the same file

Code: Select all

action=""
or to another directory all together

Code: Select all

action="/actions/registrations/"
where you handle all of your form submissions? My concerns with the former are that the code can get really long and scattered if there are numerous inputs and such. Also error handling can require many more lines only adding to the unreadability. On the plus side, I have found that it is much easier to output any errors this way. There is no URL redirection which would lose any variables or errors(...) arrays you may have set.

Posting forms to another directory just makes more sense to me, however I haven't found a reliable way to handle errors and displaying them back out to the user. A co-worker of mine likes to post his forms to an /actions/ dir, test for errors and build a GET string containing any messages to display back to the requesting file, like so

Code: Select all

if( $error ){ header("Location: /sign-up/index.php?error=Your_password_must_contain_a_number")
. But this gives me one nasty looking URL and I refuse to do it this way.

Any help on this issue would be appreciated. Want I want to do is, POST a form to an /actions/ directory and redirect the user back to the page he came from with any accompanying messages or errors that I want to display without uglifying the URL.

-Ciao
User avatar
nor0101
Forum Commoner
Posts: 53
Joined: Thu Jan 15, 2009 12:06 pm
Location: Wisconsin

Re: Form Submissions ... where to and error handeling

Post by nor0101 »

I'd throw the error message into a session variable, then once you make it back to the original page just check for it. Kinda like this:

in your /actions/action.php

Code: Select all

 
session_name("my_name");
session_start();
...
...
if ($something_is_wrong) {
    $_SESSION['err_msg'] = "Please fix your mistake.";
}
 
 
Then, back in the script of origin:

Code: Select all

 
<?php
session_name("my_name");
session_start();
...
if (isset($_SESSION['err_msg'])) {
    echo($_SESSION['err_msg']);
}
?>
 
wpsd2006
Forum Commoner
Posts: 66
Joined: Wed Jan 07, 2009 12:43 am

Re: Form Submissions ... where to and error handeling

Post by wpsd2006 »

im prefer using javascript or ajax and handle the error with php
make your code clean and easy to read .... for yourself while developing
sixes
Forum Newbie
Posts: 2
Joined: Sat Jan 24, 2009 7:12 pm
Location: Knoxville

Re: Form Submissions ... where to and error handeling

Post by sixes »

I've tried to avoid using sessions. I think it's because I was scared from using sessions when I learned PHP. Something about them not being 100% secure and able to be hijacked. However, that looks to be about as easy as pie.

JS and Ajax could be a nice first line of defense. I think I'll play around with this on my next project.

Any thoughts on using a Form class? I wonder if thats something I could use to store information and access from page to page.
User avatar
nor0101
Forum Commoner
Posts: 53
Joined: Thu Jan 15, 2009 12:06 pm
Location: Wisconsin

Re: Form Submissions ... where to and error handeling

Post by nor0101 »

Apples to acorns any class you use to handle forms is going to make heavy use of sessions.
It's easy to learn and use, if you plan to use PHP it'll pay dividends to put a little bit of time into understanding how it works. It might be a mistake to rely on the presence of javascript in production.

:? ^ my 2 cents.
Post Reply