views:

118

answers:

3

My initial strategy at developing forms was like this: the form post back to the same page and in the page handler I start by looking for POST variables and taking appropriate actions, then display the page content. It was all looking rosy in the beginning. I would place a hidden field inside my form and the page to keep track of current state and I could implement a state machine on the page to display the page in View, Edit or Add states. But 2 months later my pages have turned into hideous stateful monsters, riddled with IFs and branches, impossible to maintain, the state exchanged between page and client in hidden fields has is 'weight challenged', I just can't stand how it all turned out.

What am I doing wrong? Is this inherently hard? I'm down a slippery slope of reinventing ASP web forms? How is this done properly?

The technology behind (PHP) is irrelevant. My question is basically how to do HTTP forms that do more than simply take 2 fields and forward you to a 'thank you' page.

+1  A: 

if you want a more 'stateless' feel, check out sessions.
http://us3.php.net/manual/en/book.session.php

your method works fairly well for small-scale and independent forms, but you need something a little better for your larger web applications.

Robert Greiner
+1  A: 

You've really done nothing wrong per se, except ignore scalability. For your initial scope, the POST handing worked like a charm, but as applications grow, or more projects begin using your custom framework, more and more code gets repeated and you'll find yourself rewriting things you've already done from scratch which snowballs into a maintenance nightmare.

The solution is to use an established framework such as CodeIgniter, CakePHP, Symfony or any other so that you'll essentially never be repeating common tasks, and all your applications will have the same structure and flow. There are simple videos to get you started on any of these.

I assume you're trying to mimic the ViewState when you mention ASP.NET, which isn't really necessary. Each of these frameworks have built-in methods to handle form submission and validation - and combined with some client-side technology such as jQuery, you can build rich applications quite easily.

John Rasch
+1  A: 

This style is certainly usable up to a point; but needs some discipline. I explained my own methodology here. Note that if things get too complex, or if the team grows, it pays to use a real framework. (Django is just great!)

Javier