Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Ajax Jquery jQuery to Select Multiple CheckBox

Related Tutorials


 
 

Share on Google+Share on Google+

jQuery to Select Multiple CheckBox

Advertisement
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.

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

Learn from experts! Attend jQuery Training classes.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 6, 2010

Related Tutorials

Discuss: jQuery to Select Multiple CheckBox  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Jane
October 28, 2012
this doesn't seem to work - can you help?

I've copied the code as you have it and it doesn't run - So I made the following adjustments but when I hit submit, my checked values do not display where they should. Also, I saved the checkBoxSelect as an html file, not a php file. Thanks! HTML: <html> <head> <title>JQuery Tutorial: C</title> <script type="text/javascript" src="./jquery.min.js"></script> <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> </head> <body> <p> <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> <div id="content">The results will show up here.</div> </body> </html> PHP POST file: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?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."; } ?> </body> </html>
DMCA.com