Sunday, 20 February 2011

JQ Forms with AJAX-JSP2

This example demonstarates the use of ajaxSubmit() and pre- / post- submit callback.

Document : sample2
Created on : 19.Şub.2011, 09:56:09
Originally : belongs to "jQuery Form Plugin"
Author : Ali Riza SARAL

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>

<script type="text/javascript">
$(document).ready(function() {
var options = {
target: '#output2', // target element(s) to be updated with server response
beforeSubmit: showRequest, // pre-submit callback
success: showResponse // post-submit callback

// other available options:
//url: url // override for form's 'action' attribute
//type: type // 'get' or 'post', override for form's 'method' attribute
//dataType: null // 'xml', 'script', or 'json' (expected server response type)
//clearForm: true // clear all form fields after successful submit
//resetForm: true // reset the form after successful submit

// $.ajax options can be used here too, for example:
//timeout: 3000

// bind to the form's submit event
$('#myForm2').submit(function() {
// inside event callbacks 'this' is the DOM element so we first
// wrap it in a jQuery object and then invoke ajaxSubmit

// !!! Important !!!
// always return false to prevent standard browser submit and page navigation
return false;

// pre-submit callback
function showRequest(formData, jqForm, options) {
// formData is an array; here we use $.param to convert it to a string to display it
// but the form plugin does this for you automatically when it submits the data
var queryString = $.param(formData);

// jqForm is a jQuery object encapsulating the form element. To access the
// DOM element for the form do this:
// var formElement = jqForm[0];

alert('About to submit: \n\n' + queryString);

// here we could return false to prevent the form from being submitted;
// returning anything other than false will allow the form submit to continue
return true;

// post-submit callback
function showResponse(responseText, statusText, xhr, $form) {
// for normal html responses, the first argument to the success callback
// is the XMLHttpRequest object's responseText property

// if the ajaxSubmit method was passed an Options Object with the dataType
// property set to 'xml' then the first argument to the success callback
// is the XMLHttpRequest object's responseXML property

// if the ajaxSubmit method was passed an Options Object with the dataType
// property set to 'json' then the first argument to the success callback
// is the json data object returned by the server

alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
'\n\nThe output div should have already been updated with the responseText.');

<div id="sample2" class="sampleTabContent" data-tabid="ajaxSubmit">
<p />
The following code controls the HTML form beneath it. It uses <code class="inline">ajaxSubmit</code>
to submit the form.
<pre><code class="mix">// prepare the form when the DOM is ready

<form id="myForm2" action="dummy.jsp" method="post"><div>

<input type="hidden" name="Hidden" value="hiddenValue" />
<tr><td>Name:</td><td><input name="Name" type="text" value="MyName2" /></td></tr>
<tr><td>Password:</td><td><input name="Password" type="password" /></td></tr>
<tr><td>Multiple:</td><td><select name="Multiple" multiple="multiple">
<optgroup label="Group 1">
<option value="one" selected="selected">One</option>

<option value="two">Two</option>
<option value="three">Three</option>
<optgroup label="Group 2">
<option value="four">Four</option>
<option value="five">Five</option>
<option value="six">Six</option>

<tr><td>Single:</td><td><select name="Single">
<option value="one" selected="selected">One</option>
<option value="two">Two</option>
<option value="three">Three</option>

<tr><td>Single2:</td><td><select name="Single2">
<optgroup label="Group 1">
<option value="A" selected="selected">A</option>
<option value="B">B</option>
<option value="C">C</option>
<optgroup label="Group 2">

<option value="D">D</option>
<option value="E">E</option>
<option value="F">F</option>
<input type="checkbox" name="Check" value="1" />

<input type="checkbox" name="Check" value="2" />
<input type="checkbox" name="Check" value="3" />
<input type="radio" name="Radio" value="1" />
<input type="radio" name="Radio" value="2" />
<input type="radio" name="Radio" value="3" />
<tr><td>Text:</td><td><textarea name="Text" rows="2" cols="20">This is Form2</textarea></td></tr>

<input type="reset" name="resetButton " value="Reset" />
<input type="submit" name="submitButton" value="Submit1" />
<input type="image" name="submitButton" value="Submit2" src="submit.gif" />
<h1>Output Div (#output2):</h1>
<div id="output2">AJAX response will replace this content.</div>


Document : dummy
Created on : 19.?ub.2011, 09:30:16
Author : Ali Riza SARAL
out.print("Hello world");