<? if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form ?> <!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> <script = "text/javascript"> function CheckALL(ALL_checkbox) { // This function iterates through all checkboxes in a form and if the checkbox named 'allbox' is checked, // then it will check each individual checkbox. Otherwise if 'allbox' is unchecked, then it will uncheck // each indivudual checkbox. for (i=0; i < document.myform.elements.length; i++) { var form_element = document.myform.elements; if ((form_element.name != 'allbox') && (form_element.type == 'checkbox') && form_element.name.substr(0,1) == 'C') { if (ALL_checkbox != 1) // If the ALL checkbox is OFF, set all checkboxes to OFF (and vice-versa) form_element.checked = document.myform.allbox.checked; } } } /*function CheckALL2(ALL_checkbox) { // This function iterates through all checkboxes in a form and if the checkbox named 'allbox' is checked, // then it will check each individual checkbox. Otherwise if 'allbox' is unchecked, then it will uncheck // each indivudual checkbox. for (i=0; i < document.myform.elements.length; i++) { var form_element = document.myform.elements; if ((form_element.name != 'allbox2') && (form_element.type == 'checkbox') && form_element.name.substr(0,1) == 'C') { if (ALL_checkbox != 1) // If the ALL checkbox is OFF, set all checkboxes to OFF (and vice-versa) form_element.checked = document.myform.allbox.checked; } } }*/ function CheckboxCheck() { // This function iterates through all checkboxes in a form and counts up the total number of checkboxes, // and the total number of those checkboxes that are checked (not including the 'allbox'). If they are equal, // then the checkbox named 'allbox' is then checked, otherwise 'allbox' is unchecked. var total_checkboxes = 0; var checked_checkboxes = 0; for (i=0; i < document.myform.elements.length; i++) { var form_element = document.myform.elements; if ((form_element.name != 'allbox') && (form_element.type == 'checkbox') && form_element.name.substr(0,1) == 'C') { total_checkboxes++; // If the form element is a checkbox and it's not the 'ALL' checkbox, increment the total_checkboxes counter if (form_element.checked) checked_checkboxes++; // If the checkbox we're on is checked, add it to the checked_checkboxes total } } // If the total number of checkboxes checked equals the total number of checkboxes, put a check // in the ALL checkbox. Otherwise don't put a check in the ALL checkbox document.myform.allbox.checked = (checked_checkboxes == total_checkboxes) ? true : false; } </script> <style type="text/css"> body { margin: 20px; background: ghostwhite; } .controls th { font: bold 12px arial; text-align: left; } .controls td { padding-left: 48px; font: normal 11px arial; } .controls td.section { padding: 8px 0 0 24px; font: bold 11px arial; } .controls td.section .master { width: 60px; font: normal 9px arial; background: #eee; } </style> </head> <center> <body> <form method="post" action="<?php echo $PHP_SELF;?>" name="myform"> <?php /*?> <!-- <td> <input type="checkbox" id="delchk" name="delcheck[]" value="1" /></td>--> <!-- <td> <input type='checkbox' onclick='javascript: checkAll(true);' value='Select All'></td> <td> <input type='checkbox' onclick='javascript: checkAll(false);' value='UnSelect All'> </td> --> With Access <br /> <td><input type="checkbox" onclick='javascript: checkAll(true);' name="delcheck[]2" value=1 /></td> Full Access <br /> <input type="checkbox" onclick='javascript: checkAll(false);' name="delcheck[]" value=1 /> <input type="checkbox" name="delcheck[]" value=1 /> New<br /> <input type="checkbox" name="delcheck[]" value=1 /> Edit<br /> <input type="checkbox" name="delcheck[]" value=1 /> Delete<br /> <input type="checkbox" name="delcheck[]" value=1 /> Cancel<br /> <input type="checkbox" name="delcheck[]" value=1 /> Print<br /> <input type="checkbox" name="delcheck[]" value=1 /> Disapprove<br /> <input type="checkbox" name="delcheck[]" value=1 /> Reference<br /> </p> <?php */?> <table width="456" border="1" cellpadding="0" cellspacing="0"> <!--DWLayoutTable--> <tr align="center"> <td width="250" height="24" valign="top">Module Name </td> <td width="105" valign="top">With Access </td> <td width="105" valign="top">Full Access </td> <td width="112" valign="top">New </td> <td width="127" valign="top">Edit </td> <td width="127" valign="top">Delete </td> <td width="127" valign="top">Cancel </td> </tr> <tr align="center"> <td height="23" valign="top"></td> <td><input type="checkbox" name="allbox" value="ON" onclick="CheckALL(this)"></td> <td><input type="checkbox" name="C[]" value="1" onclick="CheckboxCheck()"></td> <td><input type="checkbox" name="C[]" value="1" onclick="CheckboxCheck()"></td> <td><input type="checkbox" name="C[]" value="1" onclick="CheckboxCheck()"></td> <td><input type="checkbox" name="C[]" value="1" onclick="CheckboxCheck()"></td> <td><input type="checkbox" name="C[]" value="1" onclick="CheckboxCheck()"></td> </tr> </table> <td class=\"valid\" ></td> <td class=\"valid\" ></td> <hr> <input type="submit" value="Submit" name="submit"> <!--<input type='button' onclick='javascript: checkAll(true);' value='Select All'> <input type='button' onclick='javascript: checkAll(false);' value='UnSelect All'></body> </ --> <? } else { mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("comunion") or die(mysql_error()); $media_array = $_POST['C']; foreach ($media_array as $one_media) { $source .= $one_media.""; } $media = substr($source, 0, 15); echo $media; $query = "UPDATE erp_sec_accesslevel SET access_level = $media WHERE access_level_id = 1"; $result = mysql_query($query) or die('error');*/ //echo "$media"; } ?> </body> </center> </html> I'm still new to php and javascript. The above code is what im working on. The idea is when the checkbox is checked=1 & unchecked =0 but the problem ive encountered is when i unchecked the checkbox, nothing appeared or " ". desired output when unchecked 1 button: 10111 or 01111 my output : 1111 or 1111 (value of 0 doesnt display) Could anyone help me with this? Really need to fix this prob.. (
You are right, only checked checkboxes return a value. I am also new to php, about 6 months, however I had a simular problem. After searching the internet and not coming up with an adequate solution, I had to get creative. The trick of course for me was to be able to store into a mysql db the values ruturned from checkboxes in a form. I needed a form for assigning permissions to users within a system. Take a look at the below script I used as a test. I assigned each checkbox a unique binary/decimal value, i.e. 1,2,4,8. You can copy this code and upload it to your server to check it out. It worked very well for me. <?php $myScript = $_SERVER['PHP_SELF']; if(isset($_POST['test'])) { $test = $_POST['test']; for ($i=0; $i<16; $i++) { foreach ($test[$i] as $result[$i]) { $word[$i] = $word[$i] + $result[$i]; } if ( 1 & $word[$i] ) $user[$i]['can_view'] = TRUE; if ( 2 & $word[$i] ) $user[$i]['can_add'] = TRUE; if ( 4 & $word[$i] ) $user[$i]['can_edit'] = TRUE; if ( 8 & $word[$i] ) $user[$i]['can_delete'] = TRUE; } } echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Check Box</title> </head> <body>'; echo ' <table border="1" cellspacing="0" cellpadding="1"> <tr> <td> </td> <td>Can View</td> <td>Can Add</td> <td>Can Edit</td> <td>Can Delete</td> </tr>'; echo ' <form name="form" method="post" action="', $myScript, '">'; $r = 0; for ($g=0; $g<4; $g++) { for ($sg=0; $sg<4; $sg++ ) { echo ' <tr> <td>User ', $r, '</td> <td style="text-align:center"> <input type="checkbox" name="test[', $r, '][0]" value="1" '; if($user[$r]['can_view']) { echo 'checked="checked"'; } echo '></td>'; echo ' <td style="text-align:center"> <input type="checkbox" name="test[', $r, '][1]" value="2" '; if($user[$r]['can_add']) { echo 'checked="checked"'; } echo '></td>'; echo ' <td style="text-align:center"> <input type="checkbox" name="test[', $r, '][2]" value="4" '; if($user[$r]['can_edit']) { echo 'checked="checked"'; } echo '></td>'; echo ' <td style="text-align:center"> <input type="checkbox" name="test[', $r, '][3]" value="8" '; if($user[$r]['can_delete']) { echo 'checked="checked"'; } echo '></td>'; echo ' </tr>'; $r++; } } echo ' <tr> <td style="text-align:center" colspan="5"> <input type="submit" name="submit" value="Submit"> </td> </tr> </form> </table> <br>'; print_r($user); echo ' </body> </html>'; ?> PHP:
Hi, jgithens thanks for having an interest in my post xD. I've tried ur code and its great. But my goal is retrieve the data from my table(accesslevel table) and update that field. Their field are access_id, user_access_level. Given from ur example if i check all the checkbox it must update the access_id(User 1) & user_access_level(1111)... (User 2)(1011) etc... Also coud you have a checkbox that has the function of checking all other checkbox. Thanks -Dean