views:

41

answers:

4

I've been doing a lot of reading lately about best practices when it comes to Web App design. My language of expertise is PHP/MySQL. When creating an MVC architecture for an application I'm developing, I always come across the problem of where to store invalid form submissions (so that the user doesn't have to re-enter the data, and helpful error messages can be displayed).

Usually I've settled with storing it as a serialized object in a SESSION variable, but my latest reading has told me sessions are bad and take away from stateless web development. A few alternatives I've thought of are:

  1. storing it in the database as I would with valid input, but flagging it as invalid
  2. storing it in the database in a separate table specific for invalid form submissions
  3. storing it in a text file

They all have their downsides.

  1. Not ALL forms result in the creation of a (single) database entity.
  2. and 3. both seem like the same idea as SESSION variables

What would you recommend?

A: 

If the form is publicly available and you do not have user authentification for them, you will lose the visitors when the session ends.

  • So SESSION is the one to consider.
  • If you have users you can save as a Serialized object to database.
  • The other is to use HTML5 local storage which is the best method, but only a few browser support it yet.
Pentium10
+5  A: 

Don't store it at all, just present the user with an error message and another copy of the form. Use the data you have (in $_POST/$_GET since you are working in PHP) to pre-populate the form with the previously entered data.

David Dorward
A: 

1) You can have problems with sessions if you are spreading the application across two or more servers in a round-robin or load balancing scenario but if you have no plans to scale the application larger than one server then this would not apply

2) you could pass the data back and forwards in cookies between the server and the browser. probably using a cookie array to hold the data all together. this way if the user is being round-robined in a load balancing scenario then the data will follow them round and state is irrelevant.

PurplePilot
A: 

Have a look at zend_form

It handles filling in the data for you. And you can also do automatic validation and filtering of data

Ilya Biryukov