Monday, October 06, 2008

Submit a Form with Multiple Buttons in Monorail

Here's how to submit a form with multiple buttons via MonoRail. The javascript will control which action is called in the controller. This example uses prototype to setup the event handlers:

$Form.FormTag("%{id='myform'}")
$Form.TextField("email")
<button id="addbutton">Add</button>
<button id="removebutton">Remove</button>
$Form.EndFormTag()

<script>

Event.observe(window, 'load', function() {

Event.observe('addbutton', 'click', function() {
$('myform').action = '$Url.For("%{action='addemail'}")';
$('myform').submit();
return false;
});

Event.observe('removebutton', 'click', function() {
$('myform').action = '$Url.For("%{action='removeemail'}")';
$('myform').submit();
return false;
});

});
</script>


I used http://www.manoli.net/csharpformat/ to format the code.

No comments: