Kappa Computer Solutions, LLC

Leveraging UNIX, GNU/Linux, open source, and
custom software solutions for business and beyond

Phone: 877•367•8837   Twitter: kappacs

Home arrow Articles arrow FormRequest: Dynamic Form Parameters
FormRequest: Dynamic Form Parameters PDF Print E-mail
Article Index
FormRequest: Dynamic Form Parameters
Page 2
Page 3
 

Some Solutions

Where the use of javascript is acceptable, one work-around is to omit the name attribute from the button so that no value is directly passed in the request and to use a click handler to populate a hidden input according to which button has been pressed, as follows:

<input type='hidden' name='action' value='' />
<button type='submit' onclick='this.form.action.value="Stop"'>
<img src='stop.png' height='40' width='40' alt='Stop' /></button>
<button type='submit'
onclick='this.form.action.value="Start"'>
<img src='start.png' height='40' width='40' alt='Start' /></button>

A side effect of this approach is that there will now always be at least an empty value for "action", even if the form is submitted by some other means (e.g. javascript code or another submit button not using the variable "action").

In a very complex form, there could be quite a few "conditional" parameters. Of course, it is possible to create hidden inputs in advance for every possible value you might wish to submit with the form, along with special values, additional parameters, or some other context for server-side logic to determine which values should actually be used.

A slightly more sophisticated approach (also using and dependent on javascript) is to dynamically add hidden inputs to the document object model (DOM) for exactly the values you need for a particular request.

The "formRequest" function created by Brian Katzung of Kappa Computer Solutions, LLC makes this easy and convenient.

Continuing with the example from above, the code would look something like this:

<script type='text/javascript' src='formrequest.js'></script>
...
<button type='button' onclick='formRequest(this, { "action": "Stop" })'>
<img src='stop.png'
height='40' width='40' alt='Stop' /></button>
<button type='button' onclick='formRequest(this, { "action": "Start" })'>
<img src='start.png'
height='40' width='40' alt='Start' /></button>

In this example, the call to formRequest will dynamically add a hidden input with name "action" and a value of "Start" or "Stop", depending on which button is pressed. Note that the button type is now "button", not "submit", as formRequest will handle submitting the form. Like the original example, the "action" input will not exist in the request unless one of these buttons is used to submit the form or it is created elsewhere by other means.



 
Next >