sw45acp Posted June 16, 2010 Share Posted June 16, 2010 Hi, I have a checklist that is generated from a server that allows users to check stuff off as they get them done. Because each user will have a different number of checkboxes, I use an array in the checkbox name like we are all supposed to do and put the id of that item inside the brackets when its generated from the server, like this: <input type="checkbox" name="item[9]" value="1" /> 1 stands for the item being complete, 0 stands for the item being incomplete And I process it like this: $item_list = $_POST['item']; foreach($item_list as $id => $item) { $q = mysql_query("UPDATE completion SET `complete` = '$item' WHERE `id` = '$id'"); } The problem here is that when someone decides that an item is not complete and unchecks it, the loop skips over it, and it still stays as complete. That is what I'm trying to fix. Thank you for any help Quote Link to comment https://forums.phpfreaks.com/topic/204945-php-and-checkboxes/ Share on other sites More sharing options...
bugcoder Posted June 16, 2010 Share Posted June 16, 2010 The reason of this that i am getting is that you have hardcoded value=1. I think you should remove this attribute of value=1 and you will be able to get values of checkboxes either checked or not checked. Quote Link to comment https://forums.phpfreaks.com/topic/204945-php-and-checkboxes/#findComment-1072961 Share on other sites More sharing options...
sw45acp Posted June 16, 2010 Author Share Posted June 16, 2010 while that is true, what do I do if the item is already checked as complete when it is written to the page. I have it set so that if it is already complete it is checked off. If the person decides its not complete, and unchecks it, php will not recognize a checkbox that is unchecked so it will skip over it Quote Link to comment https://forums.phpfreaks.com/topic/204945-php-and-checkboxes/#findComment-1072966 Share on other sites More sharing options...
sw45acp Posted June 16, 2010 Author Share Posted June 16, 2010 Let me show you an example of what I mean. From the new grand theft auto IV video game, there is a checklist to complete stuff (missions, etc). http://www.gta4.tv/100percent.php This website has it set up so users can check off what they have done. What if they accidently checked off one by mistake? So naturally you would uncheck it because it is not complete and resubmit, but php does not recognize checkboxes that are not checked. Sorry I'm really bad at wording things. Quote Link to comment https://forums.phpfreaks.com/topic/204945-php-and-checkboxes/#findComment-1072967 Share on other sites More sharing options...
bugcoder Posted June 16, 2010 Share Posted June 16, 2010 Instead of looping on $_POST data first bring record from table and loop on it and inside that loop check $_POST by in_array and if returns true consider it one else zero. Quote Link to comment https://forums.phpfreaks.com/topic/204945-php-and-checkboxes/#findComment-1072975 Share on other sites More sharing options...
sw45acp Posted June 16, 2010 Author Share Posted June 16, 2010 would it be too much to ask if you could provide an example to start me off? thank you so much. Quote Link to comment https://forums.phpfreaks.com/topic/204945-php-and-checkboxes/#findComment-1072977 Share on other sites More sharing options...
bugcoder Posted June 16, 2010 Share Posted June 16, 2010 i will give you outline but cant give exact code due to lack of programing in structured php as i use cakephp $result = mysql_query("select * from completion"); $item_list = $_POST['item']; //then loop on its result like for(.......){ // get id here of record if(in_array($id,$item_list) ){ //its 1 here }else{ //its 0 here } } Quote Link to comment https://forums.phpfreaks.com/topic/204945-php-and-checkboxes/#findComment-1072980 Share on other sites More sharing options...
sw45acp Posted June 16, 2010 Author Share Posted June 16, 2010 Ok, thank you, I'll work on this for a while. Quote Link to comment https://forums.phpfreaks.com/topic/204945-php-and-checkboxes/#findComment-1072983 Share on other sites More sharing options...
bugcoder Posted June 16, 2010 Share Posted June 16, 2010 might be you have to use array_key_exists instead of in_array. Quote Link to comment https://forums.phpfreaks.com/topic/204945-php-and-checkboxes/#findComment-1073002 Share on other sites More sharing options...
sw45acp Posted June 17, 2010 Author Share Posted June 17, 2010 I just wanted to reply to you again and say thank you for your help. You're outline pointed me in the correct direction and yes I had to use array_key_exists() instead of in_array(). It is very annoying that unchecked checkboxes are not sent to the php script, and because they are all dynamically generated, making a hidden field for each one would be impossible. This was my finished code, and it works great! $item_id = $_POST['item_id']; $q = mysql_query("SELECT * FROM table"); //create an array to hold keys (id of each checklist item) from database $array_keys = array(); while($rows = mysql_fetch_array($q)) { /*make it an associative array, filling it with the id of the item as its key, and the value as blank*/ $array_keys[$rows['id']] = ''; } //now its time to do the comparison with the array from the database and from the form foreach ($array_keys as $key => $value) { if (array_key_exists($key,$item_id)) { /*check if the key exists from the database in the form array 'haystack' if it does, and its id and a value of 1 (meaning its complete), to a new array, if not, and its id and a value of 0*/ $new_array[$key] = '1'; }else{ $new_array[$key] = '0'; } } //print_r($new_array); foreach($new_array as $id=>$complete) { //here, for every checkbox, update database } Quote Link to comment https://forums.phpfreaks.com/topic/204945-php-and-checkboxes/#findComment-1073239 Share on other sites More sharing options...
gizmola Posted June 17, 2010 Share Posted June 17, 2010 What you're complaining about is the design of the checkbox form element in HTML. Checkboxes only get sent when checked and have no associated value. I didn't check this, but chances are the browser doesn't even send them to the server. Quote Link to comment https://forums.phpfreaks.com/topic/204945-php-and-checkboxes/#findComment-1073246 Share on other sites More sharing options...
[email protected] Posted June 23, 2010 Share Posted June 23, 2010 Here am having similar, can u help me out. below is my code <? // $i = 1; // echo "<tr style=\"height: 30px;\"><td valign=\"bottom\" style=\"border-bottom: 1px solid black;\">New Client</td><td valign=\"bottom\" style=\"border-bottom: 1px solid black;\"><input type=\"checkbox\" name=\"".intval($i)."\" id=\"".intval($i)."\"></td></tr>"; // $i++; // print_r($data); for($i = 0; $i < count($arr); $i++) { // if ($i == 5) echo '<tr style="height: 80px;"><td valign="middle" align="left"><b><u>Campaign Setup:</u></b></td></tr>'; if ($i == count($arr) - echo '<tr style="height: 60px;"><td valign="bottom" align="left"><b><u>Completion:</u></b></td></tr>'; // if ($i == echo '<tr ><td style="height: 60px;" valign="bottom"><br>Check to see if the client already has listings at the following sites and if they don\'t, create them. Make sure all URLs link back to client\'s website:<br><br></td><td></td></tr>'; if ($i >=8 && $i <= 10) $borderSize = 0; else $borderSize = 1; $text = $arr[$i]; if ($text[0] == "@") { $text = substr($text, 1); if ($data["code"][intval($i)] == 1) { $checkedY = " checked "; $checkedN = "checked"; $script = $script."\nYesOrNo(Y".intval($i)."); "; } else { $checkedN = " checked "; $checkedY = "checked"; } $control = '<table border="0"> <tr> <td valign="bottom"> Yes <input type="checkbox" '.$checkedY.' name="'.intval($i).'" id="Y'.intval($i).'" value="1" onclick="YesOrNo(this);"> </td> <td style="min-width: 10px;"><div id="none"></div></td> <td valign="bottom"> No <input '.$checkedN.' type="checkbox" name="'.intval($i).'" id="N'.intval($i).'" value="0" onclick="YesOrNo(this);"> </td> </tr> </table>'; } else { $control = "<input onclick=\"checkCondition();\" type=\"checkbox\" value=\"1\" name=\"".intval($i)."\" id=\"".intval($i)."\" "; //echo intval($data["code"][intval($i)]); if ($data["code"][intval($i)] == "1") $control=$control." checked "; $control = $control.">"; } echo "<tr id=\"tr_".intval($i)."\" style=\"height: 50px;\"><td valign=\"bottom\" style=\"border-bottom: ".$borderSize."px solid black;\">".$text."</td><td valign=\"bottom\" style=\"border-bottom: ".$borderSize."px solid black;\">$control</td></tr>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/204945-php-and-checkboxes/#findComment-1076254 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.