[nycphp-talk] Proper Form Processing Techniques
Michael B Allen
ioplex at gmail.com
Sun May 18 01:43:01 EDT 2014
Hi All,
Occasionally I need to do a little web programming and I must admit
after almost 2 decades I still find myself searching for a better way
to handle HTML forms. I blame this mostly on the W3C's invalid
assumption that HTML is for rendering documents and not building
applications. But putting blame aside, I would like to ask the list to
share their best form processing techniques.
There's a particular scenario that bugs me about forms which is that
it is increasingly rare that you have a bunch of fields with just a
submit button. There are usually multiple possible operations that
build up and modify the data before it's submitted. A good example of
this is a shopping cart where there is one form but commands for
removing and item, updating quantities, applying a discount code and
submitting the cart. But the form only has one action.
Currently I've been just using hidden fields and then call a
javascript function to fill in the hidden fields with the desired data
for the particular command and submit the form.
For example:
...
<head>
<script type="text/javascript">
function cart_remove(itemid) {
document.cartform.cmd.value = 'remove';
document.cartform.itemid.value = itemid;
document.cartform.submit();
}
function cart_update_quantities() {
document.cartform.cmd.value = 'update';
document.cartform.submit();
}
...
</script>
</head>
<form name="cartform" action=/purchase" method="post">
<input name="tok" type="hidden" value="0eb3397aa9a4055a"/>
<input name="cmd" type="hidden" value="next"/>
<input name="itemid" type="hidden" value=""/>
...
<input name="quantity_3756" type="text" value="1"/>
<a href="javascript:cart_remove(3756)">Remove</a>
...
<a href="javascript:cart_update_quantities()">Update Qty.</a>
...
</form>
So how would you do this sort of thing?
My current technique seems a little hackish because I'm using the
hidden form element "cmd" to modify the action. In practice it might
seem purer to modify the action to call /purchase/remove or
/purchase/update for use with modern routing on the server.
Can you point me to a site that you think illustrates good form
processing technique?
Note that I'm not really interested in frameworks and third party
components. This cart example is just one example. I'm really trying
to arrive at a proper form processing technique in general. So I'm not
really interested in "Just use Acme Cart 2000" sorts of answers.
Ideas?
Mike
--
Michael B Allen
http://www.ioplex.com/
More information about the talk
mailing list