Sunday, 20 February 2011

JQ Forms with AJAX-JSP1

This tutorial is based on the examples given at "jQuery Form Plugin" page. I tried to remain as loyal as possible to the originals which belong to the official JQuery site.

My contribution although not big is:
1- I extracted the examples from the gorgious example frame of JQuery and made them simple easy to copy and use seperate examples.

2- I changed the PHP AJAX calls to JSP AJAX calls.

Kind regards.

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

Document : sample1
Created on : 19.Şub.2011, 09:19:43
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">
// wait for the DOM to be loaded
$(document).ready(function() {
var options = {
target: '#output1', // 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 form using 'ajaxForm'

// 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 ajaxForm 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 ajaxForm 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="sample1">
<form id="myForm1" action="dummy.jsp" method="post"><div>
<input type="hidden" name="Hidden" value="hiddenValue" />
<tr><td>Name:</td><td><input name="Name" type="text" value="MyName1" /></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 Form1</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" />
<input type="image" name="submitButton" value="Submit3" src="submit.gif" />
<input type="image" name="submitButton" value="Submit4" src="submit.gif" />
<button type="submit" name="submitButton" value="Submit5"><span>submit 5</span></button>
<h1>Output Div (#output1):</h1>
<div id="output1">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");