Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check Box Array Validation

Status
Not open for further replies.

wigglebrother

Technical User
Feb 20, 2008
1
US
Below is code that provides the checks I need, inserts variables into a checkbox array and checks to see if at least one checkbox is selected. The problem is that the values attached to the checkbox are not carried forward to the print section. If part 2 is selected, the output is


Here We Are.
P choice 0
a choice 1
r choice 2
t choice 3
choice 4
2 choice 5

whereas I need it to output blanks for everything but choice 2.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<script Language="javascript">
function CheckRB_onsubmit() {
var field = document.forms["partsform"].elements["Choice"];
for (i=0; i< field.length; i++){
if (field.checked==true)
return true;
}
alert("You must select either Part1, Part2 or Part3");
return false;
}
</script>
</head>
<body>
<?php
$loop_counter = 0;
$part_counter = 4;
?>
<table>
<form name="partsform" method="post" action="test2.php?authorize=entered" onSubmit="return CheckRB_onsubmit()">

<?php
while($loop_counter < $part_counter)
{
?>
<tr>
<td>
Part<?php echo $loop_counter;?> <input name="Choice" type="checkbox" value="Part <?php echo $loop_counter;?>">
</td>
</tr>
<?php
++$loop_counter;
}
?>
</table>
<input name="parts" type="submit" id="parts" value="Select Part">

</form>
<?php

echo $authorize;
if($authorize == entered)
{
echo"Here We Are.<BR>";

echo $Choice[0]." choice 0<br>";
echo $Choice[1]." choice 1<br>";
echo $Choice[2]." choice 2<br>";
echo $Choice[3]." choice 3<br>";
echo $Choice[4]." choice 4<br>";
echo $Choice[5]." choice 5<br>";
}
?>

</body>
</html>



If the code is modified as shown below, (Choice value with loop counter) the check for checkboxes fails but the output is what I am looking for in that is echoes and carries forward the parts selected:

Part<?php echo $loop_counter;?> <input name="Choice[<?php echo $loop_counter;?>]" type="checkbox" value="Part <?php echo $loop_counter;?>">


Here We Are.
choice 0
choice 1
Part 2 choice 2
choice 3
choice 4
choice 5

How do I form a checkbox array, check for at least one box being checked, yet carry the values forward for utilization later in the scripts?

TIA

Joe
 
When you submit a form, values for a checkbox do not get submitted unless they are checked. There's no way to get around that. However, what you could do in the onsubmit handler of the form is grab all the values that are checked and store the values in a hidden input, and then check all the boxes before the form is submitted. Or, you could store all possible values in the hidden input, and only submit what was checked. I'm afraid those will be your 2 best options, and neither of them will be reliable in the event that a user disables javascript.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top