valency Posted May 10, 2008 Share Posted May 10, 2008 I can use some help in figuring out what I am doing wrong here or need to do. I am populating an array of checkboxes from the database of userID's. Any checkbox can be checked all even all based on the javascript code that I wrote for that, now I want the checkbox that are checked remain checked after the post which updates the database in another column. Here is the code so far: <? echo "<form action=\" \"method=\"POST\" name=\"theForm\"><table border=2>"; echo "<tr><th>CheckBox</th><th>Username</th><th>Password</th><th>First name</th><th>Last name</th><th>Email</th></tr>"; if ($tr > 0) { for ($cc = 0; $cc<$tr; $cc++) { $row = $db->results[$cc]; echo "<tr><td><input type=\"checkbox\" name=\"id[]\" value=\"".$row->userID."\"><td> ".$row->username."</td><td> ".$row->password." </td><td> ".$row->firstName." </td><td>".$row->lastName." </td><td> ".$row->email." </td></tr>\n"; } } echo "</table><input type=\"submit\" value=\"Click here to delete the selected accounts \"></form>"; ?> So farthis code does exactly what i Want it to do, now I want to keep the checkbox checked for each userID value boxchecked after a post or submit is set. I have tried lotsof methods including it inside the loop functions,but all wont keep the check box checked...Please help out especially in regards to this code since all other blogs were not this code specific. Quote Link to comment Share on other sites More sharing options...
xenophobia Posted May 10, 2008 Share Posted May 10, 2008 I steal your code and rewrite on my own text editor. Try these out. Hope you understand the code. for ($cc = 0; $cc<$tr; $cc++) { $row = $db->results[$cc]; $check_value = ""; if($_SERVER['REQUEST_METHOD'] == "POST"){ // So the form is posted! if(isset($_POST['id']) && sizeof($_POST['id']){ foreach($_POST['id'] as $val){ if($val == $row->userID) { // Ahh I found the checked value! $check_value = "checked=\"checked\""; } } } } echo "<tr> <td> <input type=\"checkbox\" $check_value name=\"id[]\" value=\"".$row->userID."\"><td> ".$row->username." </td> <td> ".$row->password." </td> <td> ".$row->firstName." </td> <td>".$row->lastName." </td> <td> ".$row->email." </td> </tr>\n"; } Quote Link to comment Share on other sites More sharing options...
valency Posted May 10, 2008 Author Share Posted May 10, 2008 Cant wait to try this out, xeno, I appreciate the help, will give u a feedback if allgoes well on my dev. Quote Link to comment Share on other sites More sharing options...
valency Posted May 12, 2008 Author Share Posted May 12, 2008 Hey Xeno, the code worked right, the checked box stayed checked but when the update first occurs with the checked value, when u uncheck the value, it does not update after resubmitting. Do I need to include another if in the Update value or what? The code below is a continuation from the previously posted problem with checkboxes! <? //if the data gets posted, set the ID row to the checkboxes if (isset($_POST['id'])) { $checkboxofid = sizeof($_POST['id']); //count the variables $updated = 0; $inserted = 0; for ($ii=0;$ii<$checkboxofid; $ii++) { $queryupdate = 'UPDATE users SET excludeFromD = 1 WHERE userID = '.$_POST['id'][$ii]; $result3 = $db->query($queryupdate); //echo $queryupdate, "<br>"; $updated++; } echo "<BR />".$updated." account(s) updated (excluded from designs only)<br /><br />"; } ?> Quote Link to comment 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.