how to find out if a check box was unticked? :/

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

how to find out if a check box was unticked? :/

Post by qads »

Hi,
is there any way i can fix this?
if you save this code in a .php file and run it, tick a check box and it will add its value in the text box, but if you untick the checkbox it still adds the value in the text box, it should remove it not add it :P.

you really have to see the code in action i guess to see what i mean :/.

thanks for you help in adv :).

Code: Select all

<?php<?php
function open($title)
{
echo '<table border="0" width="50%" style="border: 1 solid #000000" cellspacing="0" cellpadding="0">
  <tr>
    <td width="100%" style="background-color: #0099FF; color: #FFFFFF; font-family: Tahoma; font-size: 12pt; letter-spacing: 1pt; vertical-align: 1; font-weight: bold; border-bottom-style: solid; border-bottom-width: 1">'.$title.' [+]</td>
  </tr>
  <tr>
      <td width="100%" style="font-family: Verdana; font-size: 8pt; color: #000000; text-align: Left; margin-left: 4">';
}
function close_table()
{
echo '</td>
  </tr>
</table>';
}
?>
<!--
     <script type="text/javascript"> 
         function checkAll() 
         { 
            oColl = document.getElementsByName("check[]"); 
            for (i=0; i!=oColl.length; i++) 
               oColl[i].checked = "true"; 
         } 
      </script>

<script language="Javascript" type="text/javascript"> 
    <!-- 
      function changeTextBox(form, newValue, name) 
      {
	 form.hidden.value = form.hidden.value + newValue; 
      } 
    //--> 
    </script>
	
<title>Check Boxs</title>

<form action="check_boxs.php" name="checkbox" method="post">
<?php
open("Check Boxs");
$num = 5;
for($x=1; $x<=$num; $x++)
{
echo "&nbsp;Check Box $x: <input type="checkbox" name="check$x" value="$x" onClick="changeTextBox(this.form, '$x|', 'check$x')" />\n<br />";
}
?>
<div align="right">
    <input name="submit" type="submit" value="Submit ->">
</div><?php
close_table();
?>
<input type="text" name="hidden" value="">
</form>

<?php
if(isset($_POST[submit]))
{
echo "<br />";
open("Results");
$hidden = explode("|",$_POST[hidden]);
$array = array_unique($hidden);
for($r=0; $r<=count($array); $r++)
{
if(!empty($array[$r]))
{
echo "Check Box $r: ".$array[$r]."<br />";
}
}
close_table();
}
?>

?>
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

of course it can be changed, but the question is why are you assigning the values into the hidden field? why dont you work with the check box values directly in the processing section?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

uh...cos i want to? :P..lol....i don't know, i just made this and it seems to be working expect the untick part :/...this is just a test page, the code will be implaneted into a much bigger application where the form will submit the data to 3/4 pages so it seems like a good idea to have the values in group...in a hidden field in this case :). saves alot of $_POST etc.

if you know how to do it then that would be cool.

thanks
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

Code: Select all

<script language="Javascript" type="text/javascript"> 
    <!-- 
      function changeTextBox(form, newValue, name) 
      &#123; 
         whichCheckbox = newValue.substr(0,(whichCheckbox.length-1));
         if (form.whichCheckbox.checked == true) &#123;
             form.hidden.value = form.hidden.value + newValue; 
         &#125; else &#123;
             form.hidden.value = form.hidden.value.replace(newValue,'');
         &#125;
      &#125; 
    //--> 
</script>
something like that should do it, I havent had time to test it, if you are planning on the checkboxes being named after $x and expect more than 10 checkboxes then you are going to have to use regexp so you dont replace 12| when you want to replace 2|
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

ok, thanks :).
Post Reply