|
Form Confirmer This JavaScript program displays the contents of a form and enables the user to cancel the submission if they don't like what they see. Use the following form to give it a whirl. To accomplish this, the <FORM> tag uses the JavaScript onSubmit attribute to specify a JavaScript function to run when the user submits the form. Here's the <FORM> tag for the example: <FORM ACTION="http://www.mcfedries.com/scripts/formtest.asp" METHOD="POST" NAME="Monikers" onSubmit="return validate(Monikers)">In this case, the validate function is called. Note, too, that the name of the form (Monikers, in the example) is passed as a parameter. Here's the JavaScript for this function:
<SCRIPT LANGUAGE="JavaScript">
<!--
function validate(frm) {
//
// Build the form data
//
var formData
formData = "First Name: " + frm.First.value + "\n"
formData = formData + "Last Name: " + frm.Last.value + "\n"
formData = formData + "Nickname: " + frm.Nick.value + "\n"
formData = formData + "Stage Name: " + frm.Stage.value + "\n"
//
// Show the data to the user
//
return confirm("Here is the form data you entered:" + "\n\n"
+ formData + "\n"
+ "Do you want to submit this data?")
}
//-->
</SCRIPT>
This function uses the formData variable to store that values entered into each text box. The value is
given by frm.Name.value, where Name is the name of the form field. (All those
"\n" things tell the browser to display the next bit of data on a new line.) Finally, the confirm
function is called. This function displays the data in a dialog box and asks the user if they want to submit it.
If they click OK, the data is submitted; if they click Cancel, instead, they're returned to the form.
Copyright © 1995-2008 Paul McFedries and Logophilia Limited |