Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

Hot Searches
struts-config.xml  web.xml  Java date format  read/write text file using javascript  ArrayList  Insert Data into Database Using Hibernat  create text file using java  insert data to text file using java  Visual Basic Date and Time Display the t  Create table and insert data by sql quer 

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

jQuery to Select Multiple CheckBox

                         

In this first jQuery tutorial we will develop a simple program that retrieves the value of selected checkbox from server and displays on the user browser. In this example we will be calling a server side PHP script . You can easily replace PHP with JSP, or ASP program.

Steps to develop the Select Multiple CheckBox  program

Step 1:

 

 

 

 

Create php file to that prints the CheckBox data . Here is the code of PHP script (postData.php).

<?php
if($_POST['checkbox_name']!="")
{
echo "You have selected: ";
foreach ($_POST['checkbox_name'] as $checkbox_name)
{

$checkbox_name= htmlspecialchars($checkbox_name, ENT_QUOTES);
echo $checkbox_name, ' ', "\n";
}
}
else
{
echo "Please select the checkbox."; 
}
?>

Step 2:

Write HTML page to call the postData.php . Create a new file (checkBoxSelect.php) and add the following code into it:

<html>
<head>
<title>JQuery Tutorial: C<title>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
  <script type="text/javascript">
	$(document).ready(function()
	{
	$("#submit").click(
	function()
	{
	var query_string = '';
				
	$("input[@type='checkbox'][@name='checkbox_name']").each(
	function()
	{
	if(this.checked)
		{
			query_string += "&checkbox_name[]=" + this.value;
				}
				});
		$.ajax(
	        {
			type: "POST",
		   	url: "postdata.php",
		   	data: "id=1" + query_string,
		   	success: 
		        function(t) 
			    	{
			          $("div#content").empty().append(t);
		 	   	},
		         	   	error:
				   	function()
				   	{
				    	$("div#content").append("An error 
                                           occured during processing");
				   	}
				        });	
					});
		});
		</script>
		
<span style="color:#00000;font-weight:bold">Select Your Languages</span><br>
		<input type="checkbox" name="checkbox_name" value="English">English<br>
		<input type="checkbox" name="checkbox_name" value="Hindi">Hindi<br>
		<input type="checkbox" name="checkbox_name" value="French">French<br>
		<input type="checkbox" name="checkbox_name" value="Japanese">Japanese<br>
	
		<input type="submit" id="submit" value='Submit' />

		<p>&nbsp;</p>
		
		<div id="content">The results will show up here.</div>
	</body>
</html>

Program explanation:

The following code includes the jQuery JavaScript library file:

<script type="text/javascript" src="jquery.js"></script>

The code

$("#submit").click

url : "
postData.php",
...

makes ajax call to postData.php file.

Following code intercepts the ajax call success and then sets the data into "content" div:

If return success then it OutPut 

When you run the program in browser it will look like following screen shot:

 

Check online demo of the application

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Tell A Friend
Your Friend Name

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.