Combining Multiple Actions on Events

We can combine mulitple actions together for events, and we can also use the same set of actions for on multiple events or controls.

In this example, we have a listbox, and we allow the user to dynamically add items to that listbox. On submitting, we want to perform the following actions:

  • Disable the Listbox (via Javascript)
  • Disable the Textbox (via Javascript)
  • Disable the Button (via Javascript)
  • Make an AJAX call to the PHP method AddListItem

The PHP method addListItem() will then proceed to add the item into the listbox, and re-enable all the controls that were disabled.

Note that what we are doing is combining multiple actions together into an action array (e.g. ActionBase[]). Also note that this action array is defined on two different controls: the button (as a Click) and the textbox (as a EnterKey).

Also note that we also add a Terminate action to the textbox in response to the EnterKey. The reason for this is that on some browsers, hitting the enter key in a textbox would cause the form to do a traditional form.submit() call. Given the way Forms operates with named actions, and especially given the fact that this Form is using AJAX-based actions, we do not want the browser to be haphazardly performing submits.

Finally, while this example uses Ajax to make that an AJAX-based call to the PHP addListItem() method, note that this example can just as easily have made the call to AddListItem via a standard Server. The concept of combining multiple actions together and the concept of reusing an array of actions on different controls/events remain the same.