i have 5 checkboex. How can I get the value of them if any of them is checked and submited into the second page. here is my html code.
<form action="test.php" method="post" onSubmit="return checkCheckBoxes(this);">
<input type="CHECKBOX" name="CHECKBOX_1" value="1">y
<input type="CHECKBOX" name="CHECKBOX_2" value="2">o
<input type="CHECKBOX" name="CHECKBOX_3" value="3">t
<input type="SUBMIT" value="Submit!">
</form>
here is Javascript code
<script type="text/javascript" language="javascript">
function checkCheckBoxes(theForm) {
if (
theForm.CHECKBOX_1.checked == false &&
theForm.CHECKBOX_2.checked == false &&
theForm.CHECKBOX_3.checked == false)
{
alert ('Please make sure to check a checkboxe!');
return false;
} else {
return true;
}
}
<开发者_JAVA技巧/script>
My dear first you need to change the names of your checkboxes to any array like this
<form action="test.php" method="post" onSubmit="return checkCheckBoxes(this);"> <input type="CHECKBOX" name="CHECKBOX[]" value="1">y <input type="CHECKBOX" name="CHECKBOX[]" value="2">o <input type="CHECKBOX" name="CHECKBOX[]" value="3">t <input type="SUBMIT" value="Submit!"> </form>
On your test.php just traverse the whole array of CHECKBOX like this
for($_POST['CHECKBOX'] as $key=>$value) { if(isset($key)) echo('check box is checked and do some thing with '.$value); else echo('check box is not checked'); }
<script type="text/javascript" language="javascript">
function checkCheckBoxes(theForm) {
if(theForm.CHECKBOX_1.checked == false &&
theForm.CHECKBOX_2.checked == false &&
theForm.CHECKBOX_3.checked == false )
{
alert ('Please make sure to check a checkboxe!');
return false;
} else {
var val=''; var arr = new Array();
if(theForm.CHECKBOX_1.checked == true){
val += theForm.CHECKBOX_1.value; arr.push(/*push val*/);
}
if(theForm.CHECKBOX_2.checked == true){
val += theForm.CHECKBOX_2.value;
}
if(theForm.CHECKBOX_3.checked == true){
val += theForm.CHECKBOX_3.value;
}
alert(val); //echo out array elements
}
}
</script>
<form action="test.php" method="post" onSubmit="return checkCheckBoxes(this);">
<input type="CHECKBOX" name="CHECKBOX_1" value="1"/>y
<input type="CHECKBOX" name="CHECKBOX_2" value="2"/>o
<input type="CHECKBOX" name="CHECKBOX_3" value="3"/>t
<input type="SUBMIT" value="Submit!">
</form>
[UPDATE]::
<?php
for($_POST['CHECKBOX'] as $key=>$value)
{
if(isset($key))
echo "var val[".++$count."] = '".$value."'";
}
?>
<script type="text/javascript">
for(var i=0; i<val.length; i++)
alert(val[i]);
</script>
精彩评论