![]() |
Copyright © 2008, Levyco Development, LLC. All rights are reserved. |
IntroThe demo shows how to setup a dialog submission with a confirm step using the YUI framework. You may want to start by clicking the show demo button above so you can see what we're accomplishing, then you can follow along with the commentary as we show the code.
We have a sample form which accepts some fields and has save, delete and cancel buttons. The form itself "blocks" submission by normal means -- that is,
there is no submit button and the <form onsubmit="return(false);" ...
The complete
<form onsubmit="return(false);" name="frmSample">
<table>
<tr><td class="FORMLABEL">First Name:</td><td class="FORMFIELD"><input type="text" name="firstname" value="" class="FORMINPUT"></td></tr>
<tr><td class="FORMLABEL">Last Name:</td><td class="FORMFIELD"><input type="text" name="lastname" value="" class="FORMINPUT"></td></tr>
<tr><td class="FORMLABEL">Address:</td><td class="FORMFIELD"><input type="text" name="addr1" value="" class="FORMINPUT"></td></tr>
<tr><td class="FORMLABEL">Address (cont'd):</td><td class="FORMFIELD"><input type="text" name="addr2" value="" class="FORMINPUT"></td></tr>
<tr><td class="FORMLABEL">City:</td><td class="FORMFIELD"><input type="text" name="city" value="" class="FORMINPUT"></td></tr>
<tr><td class="FORMLABEL">State/Provence:</td><td class="FORMFIELD"><input type="text" name="st" value="" class="FORMINPUT"></td></tr>
<tr><td class="FORMLABEL">ZIP/Postal Code:</td><td class="FORMFIELD"><input type="text" name="zip" value="" class="FORMINPUT"></td></tr>
<tr><td class="FORMLABEL">Country:</td><td class="FORMFIELD"><input type="text" name="cc" value="" class="FORMINPUT"></td></tr>
<tr><td class="FORMLABEL">Phone:</td><td class="FORMFIELD"><input type="text" name="phone" value="" class="FORMINPUTM"> ext-<input type="text" name="ext" value="" class="FORMINPUTM2"></td></tr>
<tr><td class="FORMLABEL">Email:</td><td class="FORMFIELD"><input type="text" name="email" value="" class="FORMINPUT"></td></tr>
</table>
<hr>
<button id="cmdSave" class="ACTIONBUTTON"><img src="<?=/images/?>t_chk.gif"> Save</button>
<button id="cmdDelete" class="ACTIONBUTTON"><img src="<?=/images/?>t_del.gif"> Delete</button>
<button id="cmdCancel" class="ACTIONBUTTON"><img src="<?=/images/?>no.gif"> Cancel</button>
<input type="hidden" name="action" value="">
<input type="hidden" name="somethingelse" value="">
</form>
So we need to handle the subission ourselves. We want to be to determine which button was pressed for any actions going to the server. We'll hook the three buttons with the following code:
YAHOO.util.Event.addListener("cmdSave", "click", function(){YConfirm('Are you sure you want to save this record?',function(){sendForm(document.frmSample,'save');});});
YAHOO.util.Event.addListener("cmdDelete", "click", function(){YConfirm('Are you sure you want to delete this record?',function(){sendForm(document.frmSample,'delete');});});
YAHOO.util.Event.addListener("cmdCancel", "click", function(){frmDemo.hide();});
We're simply asking YUI to call a function when each of the buttons is clicked. In this case, we're declaring the function inline, but this could also be a function
name (without the () after the name) to reference a function declared elsewhere on the page. In the first two cases, save and delete, we are simply going to ask the
user if they are sure, and if so, we're going to send the form data and the "action" the user is performing.
In either case, if the user selects "yes", the
This demo does not actually submit the form. However, submitting the form is very easy. The <form> tag would need an action and method
attribute to determine where and how to transmit the data, and we would set some hidden variables to include our "action" flag (and any additional
data we wanted to send.) A "functional"
function sendForm (frm, action) {
frm.action.value = action;
frm.somethingelse.value = 'YUI is awesome!';
frm.submit();
}
That's it! We now have a dialog we can popup and submit under our control. |