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>
this doesn't seem to work - can you help?Jane October 28, 2012 at 10:40 AM
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>
Post your Comment