views:

38

answers:

4

Our site has some very complicated form controls that are submitted and then show up again on the results page in case users want to resubmit the form from the results page.

Currently, this requires a lot of if-then statements to preset the form controls to what was submitted.

I'm wondering if there is a library (preferably jquery) that can take the cgi parameters from the URL and then update the form to those values onready. Or if there is a slicker way that I'm not seeing even better. We use perl as our programming language.

+1  A: 

You can use this

$('#myId').load('myCGI.py');

This

Load data from the server and place the returned HTML into the matched element.

See jquery load.

Topera
Topera, not sure how that answers my question. The user is on a form, clicks to go to another page, and the form is shown again. I want to preset the form with the parameters chosen on the previous page.
Sean
If you refresh the page, you don't need do this with javascript. This can be done in server. I give you answer using javascript to make a ajax call.
Topera
Sean
A: 

This SO answer describes a little plugin that can either capture a form's state, or apply a serialized state to a form. Perhaps this could be part of the solution.

Ken Redler
Thanks, the term deserialize let me on a fruitful search.
Sean
+1  A: 

http://www.reach1to1.com/sandbox/jquery/testform.html

This plugin takes JSON data and updates the form as desired.

This plug creates JSON from the cgi parameter string http://benalman.com/code/projects/jquery-bbq/examples/deparam/

Put them together and add a class to any forms you want to apply this to and here you go in a one-liner to run on doc ready.

function form_presetFromQueryString(){
    $('form.deserialize').deserialize( $.deparam.querystring() );
}
Sean
I've added a filter to deserialize.js $('input,select,textarea',me) .filter(':not(.no_ds)') so that I can add a class no_ds to the form so that this will not be updated by the deserialization of the query param
Sean
+1  A: 

Persisting form state isn't usually approached from the client (JS) side, but from the server side. There are a number of good modules on CPAN for dealing with web forms, including HTML::FormFu and Form::Sensible, although personally I prefer HTML::FormHandler. With it, you can declare your fields and all of their validation rules, and the module will validate the form for you, organize any errors on a per-field basis, and help you to render all of the form controls (including any error messages). You get your choice of control over rendering, from a totally automatic layout (nice for development), to manual layout with FormHandler's builtin widgets, to providing custom templates for the widgets, to doing all of the templating yourself and just using the form object as a repository of field values and messages.

hobbs