943,923 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1998
  • PHP RSS
Feb 27th, 2008
0

Checkbox Arrays ARGH!!

Expand Post »
Hello all,
I have a form that contains checkboxes to update a database with either "Yes" or "No" based on the user selection.
The check boxes are in an array with the values set to a "LinkID"
example:
php Syntax (Toggle Plain Text)
  1. <?php
  2. //find the user's assigned links
  3. $rowlink=$row_rsUpdate['LinkID'];
  4. mysql_select_db($database_mambo, $mambo);
  5. $query_rsAssignedLinks = "SELECT * FROM assignedlinks WHERE username = '$username' and ALID = '$rowlink' and `Add` ='Yes'";
  6. $rsAssignedLinks = mysql_query($query_rsAssignedLinks, $mambo) or die(mysql_error());
  7. $row_rsAssignedLinks = mysql_fetch_assoc($rsAssignedLinks);
  8. $totalRows_rsAssignedLinks = mysql_num_rows($rsAssignedLinks);
  9. ?>
  10. <input type="checkbox" name="Add[]" value="<? echo $row_rsUpdate['LinkID']; ?>"
  11. <?php if (!$_POST && (!(strcmp($row_rsAssignedLinks['Add'],"Yes")))) { echo "checked=\"checked\"";} ?> <?php if ($_POST && (isset($_POST['Add'])=="1")) { echo "checked=\"checked\"";} ?> <?php $OK = isset($_POST['Add']) ? true : false; if($OK && $_POST['Add']=="Yes") { ?> <? } ?>/>
  12. Add

I have the following insert statement that works as long as the values are "Yes":
php Syntax (Toggle Plain Text)
  1. if((isset($_POST["Submit"])) && ($_POST["MM_Update"] == "update"))
  2. {
  3. for($m=0;$m<count($_POST['Add']);$m++)
  4. {
  5. $add=$_POST['Add'][$m];
  6. //find the correct MenuID
  7. mysql_select_db($database_mambo, $mambo);
  8. $query_rsMenuID = "SELECT * FROM `links` WHERE LinkID = '$add'";
  9. $rsMenuID = mysql_query($query_rsMenuID, $mambo) or die(mysql_error());
  10. $row_rsMenuID = mysql_fetch_assoc($rsMenuID);
  11. $totalRows_rsMenuID = mysql_num_rows($rsMenuID);
  12. $mid=$row_rsMenuID['MenuID'];
  13. //find if there is already an assignedlink record written for the ALID
  14. mysql_select_db($database_mambo, $mambo);
  15. $query_rsALFind = "SELECT * FROM assignedlinks WHERE mambo_user_id = '$id' and `username` = '$username' AND ALID = '$add' AND `Add` = 'Yes'";
  16. $rsALFind = mysql_query($query_rsALFind, $mambo) or die(mysql_error());
  17. $row_rsALFind = mysql_fetch_assoc($rsALFind);
  18. $totalRows_rsALFind = mysql_num_rows($rsALFind);
  19.  
  20. (!empty($_POST["Add"]))
  21. {
  22. while(list($k, $v) = each($_POST["Add"]))
  23. {
  24. $v= "Yes";
  25.  
  26. foreach($_POST['Add'] as $row=> $assigned)
  27. {
  28. $assigned= $assigned;
  29. $MenuID = $_POST['menu'][$row];
  30. $updateAddlinksYes = "UPDATE assignedlinks SET `Add`='$v' WHERE ALID='$assigned' AND `mambo_user_id`= '$id'";
  31. $ResultAddlinksYes = mysql_query($updateAddlinksYes, $mambo) or die(mysql_error());
  32. echo $updateAddlinksYes;
  33. }

However, I need this to also update the datebase if the user unchecks the checkbox (BTW there are 4 checkboxes "Add", "Update" "Delete", and "View") or the checkbox has not been selected.

Can anyone point me in the right direction ?
Many thanks in advance!
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
JeniF is offline Offline
52 posts
since Aug 2007
Feb 27th, 2008
0

Re: Checkbox Arrays ARGH!!

When checkboxes are sent in requests as "on" if they are checked, if they are not checked they aren't sent at all. You're also missing an if on line 20
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Feb 27th, 2008
0

Re: Checkbox Arrays ARGH!!

Thanks for the input. I do see that the "if" is missing as I was doing the "cut and paste thing".
I understand what you are saying there, but unsure right now on how to exactly write what I need it to do..
basically the concept here is
I display the users assigned menus along with 4 checkboxes (Add, Update, Delete, View)...These checkboxes are an aray populated with the values contained within the db. Additionally, this checkbox array is given a value that is a constant within the repeating region.
ex:
php Syntax (Toggle Plain Text)
  1. <input type="checkbox" name="Add[]" value="<? echo $row_rsUpdate['LinkID']; ?>"<?php if (!$_POST && (!(strcmp($row_rsAssignedLinks['Add'],"Yes")))) { echo "checked=\"checked\"";} ?> <?php if ($_POST && (isset($_POST['Add'])=="1")) { echo "checked=\"checked\"";} ?> <?php $OK = isset($_POST['Add']) ? true : false; if($OK && $_POST['Add']=="Yes") { ?> <? } ?>/> Add <?php
  2. //find the user's assigned links
  3. $rowlink=$row_rsUpdate['LinkID'];
  4. mysql_select_db($database_mambo, $mambo);
  5. $query_rsAssignedLinks = "SELECT * FROM assignedlinks WHERE username = '$username' and ALID = '$rowlink' and `Add` ='Yes'";
  6. $rsAssignedLinks = mysql_query($query_rsAssignedLinks, $mambo) or die(mysql_error());
  7. $row_rsAssignedLinks = mysql_fetch_assoc($rsAssignedLinks);
  8. $totalRows_rsAssignedLinks = mysql_num_rows($rsAssignedLinks);
  9. ?>
  10. <input type="checkbox" name="Add[]" value="<? echo $row_rsUpdate['LinkID']; ?>"
  11. <?php if (!$_POST && (!(strcmp($row_rsAssignedLinks['Add'],"Yes")))) { echo "checked=\"checked\"";} ?> <?php if ($_POST && (isset($_POST['Add'])=="1")) { echo "checked=\"checked\"";} ?> <?php $OK = isset($_POST['Add']) ? true : false; if($OK && $_POST['Add']=="Yes") { ?> <? } ?>/>
  12. Add

what I want to do is:
if the user changes any values in the checkbox array, I need the db updated with all changes. I used the constant value within the array to maintain the correct record to be updated on change.
I have it working to updating and added yes values, but keep getting "stumped" on the next part...;}

any example you can thing of would me most appreciated
Last edited by JeniF; Feb 27th, 2008 at 9:50 pm. Reason: typo
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
JeniF is offline Offline
52 posts
since Aug 2007
Feb 27th, 2008
0

Re: Checkbox Arrays ARGH!!

i know i can create you some working code if i understand what you are trying to do more. do you have an online example to look at?
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Feb 27th, 2008
0

Re: Checkbox Arrays ARGH!!

Quote ...
if the user changes any values in the checkbox array, I need the db updated with all changes.
You mean, from checked to unchecked and vice versa ? As ShawnCplus has already mentioned, only those checkboxes which are 'set' will be passed on to the next page. Well, if you want to delete certain value when an checkbox is unchecked, then (what I do is), delete all the checkboxes for that particular user, then insert only checked checkboxes. So, it will indirectly delete the unselected checkboxes.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Feb 28th, 2008
0

Re: Checkbox Arrays ARGH!!

Hey there,
Yes after my "Brain-Cloud" left me for the day, I realized that I needed to delete the original values stored and reset them with the new values.
I managed to get this done.
Thanks for the "heads-up" on that one...

If anyone is interested in the code, let me know and I will post it
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
JeniF is offline Offline
52 posts
since Aug 2007
Feb 28th, 2008
0

Re: Checkbox Arrays ARGH!!

Yep. Please post your code. If anyone come across the same problem, this thread might be useful.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Feb 28th, 2008
0

Re: Checkbox Arrays ARGH!!

Here is the code that I used and tested:
php Syntax (Toggle Plain Text)
  1. <?php require_once('../Connections/mambo.php'); ?>
  2. <?php
  3. error_reporting(E_ALL ^ E_NOTICE);
  4. session_start();
  5. if((isset($_POST["Submit"])) && ($_POST["MM_Update"] == "update"))
  6. {
  7. if(!empty($_POST["Add"]))
  8. {
  9. while(list($k, $v) = each($_POST["Add"]))
  10. {
  11. $v= "Yes";
  12. foreach($_POST['Add'] as $row=> $assigned)
  13. {
  14. $assigned= $assigned;
  15. $MenuID = $_POST['menu'][$row];
  16. $updateAddlinksYes = "UPDATE assignedlinks SET `Add`='$v' WHERE ALID='$assigned' AND `mambo_user_id`= '$id'";
  17. $ResultAddlinksYes = mysql_query($updateAddlinksYes, $mambo) or die(mysql_error());
  18. echo $updateAddlinksYes;
  19. }
  20. }
  21. }
  22. ////
  23.  
  24. if(!empty($_POST["Update"]))
  25. {
  26. while(list($k, $v) = each($_POST["Update"]))
  27. {
  28. $v= "Yes";
  29. foreach($_POST['Update'] as $row=> $assigned)
  30. {
  31. $assigned= $assigned;
  32. $MenuID = $_POST['menu'][$row];
  33. $updateAddlinksYes = "UPDATE assignedlinks SET `Update`='$v' WHERE ALID='$assigned' AND `mambo_user_id`= '$id'";
  34. $ResultAddlinksYes = mysql_query($updateAddlinksYes, $mambo) or die(mysql_error());
  35. echo $updateAddlinksYes;
  36. }
  37. }
  38. }
  39.  
  40. //
  41.  
  42. if(!empty($_POST["Delete"]))
  43. {
  44. while(list($k, $v) = each($_POST["Delete"]))
  45. {
  46. $v= "Yes";
  47. foreach($_POST['Delete'] as $row=> $assigned)
  48. {
  49. $assigned= $assigned;
  50. $MenuID = $_POST['menu'][$row];
  51. $updateAddlinksYes = "UPDATE assignedlinks SET `Delete`='$v' WHERE ALID='$assigned' AND `mambo_user_id`= '$id'";
  52. $ResultAddlinksYes = mysql_query($updateAddlinksYes, $mambo) or die(mysql_error());
  53. echo $updateAddlinksYes;
  54. }
  55. }
  56. }
  57.  
  58. //
  59. if(!empty($_POST["View"]))
  60. {
  61. while(list($k, $v) = each($_POST["View"]))
  62. {
  63. $v= "Yes";
  64. foreach($_POST['View'] as $row=> $assigned)
  65. {
  66. $assigned= $assigned;
  67. $MenuID = $_POST['menu'][$row];
  68. $updateAddlinksYes = "UPDATE assignedlinks SET `View`='$v' WHERE ALID='$assigned' AND `mambo_user_id`= '$id'";
  69. $ResultAddlinksYes = mysql_query($updateAddlinksYes, $mambo) or die(mysql_error());
  70. echo $updateAddlinksYes;
  71. }
  72. }
  73. }
  74. header("Location: index.php");
  75. //end if submit
  76. }
  77.  
  78. $colname_rsUser = "-1";
  79. if (isset($_GET['id'])) {
  80. $colname_rsUser = (get_magic_quotes_gpc()) ? $_GET['id'] : addslashes($_GET['id']);
  81. }
  82. mysql_select_db($database_mambo, $mambo);
  83. $query_rsUser = sprintf("SELECT * FROM skills_access WHERE mambo_user_id = %s", $colname_rsUser);
  84. $rsUser = mysql_query($query_rsUser, $mambo) or die(mysql_error());
  85. $row_rsUser = mysql_fetch_assoc($rsUser);
  86. $totalRows_rsUser = mysql_num_rows($rsUser);
  87. $id = $row_rsUser['mambo_user_id'];
  88. mysql_select_db($database_mambo, $mambo);
  89. $query_rsUpdate = "SELECT * FROM assignedlinks, menu, links WHERE assignedlinks.mambo_user_id = '$id' AND assignedlinks.MenuID = menu.MenuID AND assignedlinks.ALID = links.LinkID";
  90. $rsUpdate = mysql_query($query_rsUpdate, $mambo) or die(mysql_error());
  91. $row_rsUpdate = mysql_fetch_assoc($rsUpdate);
  92. $totalRows_rsUpdate = mysql_num_rows($rsUpdate);
  93. $MUID= $row_rsUpdate['MenuID'];
  94. $menuname= $row_rsUpdate['menuname'];
  95. $username= $row_rsUpdate['username'];
  96.  
  97. mysql_select_db($database_mambo, $mambo);
  98. $sql= "SELECT * from assignedmenu, links WHERE mambo_user_id = '$id' AND assignedmenu.MenuID = links.MenuID AND assignedmenu.Access='Yes'";
  99. $rssql = mysql_query($sql, $mambo) or die(mysql_error());
  100. $row_rssql = mysql_fetch_assoc($rssql);
  101. $totalRows_rssql = mysql_num_rows($rssql);
  102. $username= $row_rssql['username'];
  103.  
  104. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  105. <html xmlns="http://www.w3.org/1999/xhtml">
  106. <head>
  107. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  108. <title>Update User Permissions</title>
  109. <style type="text/css">
  110. <!--
  111. body,td,th {
  112. font-family: Verdana, Arial, Helvetica, sans-serif;
  113. font-size: 12px;
  114. color: #000066;
  115. }
  116. -->
  117. </style></head>
  118. <body>
  119. <form id="update" name="update" method="post" action="">
  120. <?
  121. if (!($_POST)){
  122. ?>
  123.  
  124. <table width="800" border="1">
  125. <tr>
  126. <th colspan="6" valign="top" scope="row">
  127. <div align="center">
  128. <input name="id" type="hidden" value="<? echo $row_rsUser['mambo_user_id'];?>" />
  129. Update User Permissions for Training Modules
  130. </div></th>
  131. </tr>
  132. <tr>
  133. <th colspan="6" valign="top" scope="row"><div align="left">Permissions will be modified for :
  134.  
  135. <input name="username" type="hidden" value="<? echo $username; ?>" />
  136. <?php echo $username;?></div>
  137. </th>
  138. </tr>
  139. <tr>
  140. <th width="172" scope="row">Menu</th>
  141. <td><strong>Assigned Links</strong></td>
  142. <td colspan="4" valign="top"><div align="center"><strong>Permissions</strong></div></td>
  143. </tr>
  144. <?php do { ?>
  145. <tr>
  146. <td scope="row">
  147. <input name="MenuID[<? echo $row_rsUpdate['ALID'];?>]" type="hidden" value="<? echo $row_rsUpdate['MenuID'];?>" />
  148. <? //echo $row_rsUpdate['MenuID'];?>
  149. <? echo $row_rsUpdate['menuname'] ?>
  150. </td>
  151. <td width="228">
  152. <input name="ALID[<? echo $row_rsUpdate['ALID'];?>]" type="hidden" value="<? echo $row_rsUpdate['ALID'];?>" />
  153. <? //echo $row_rsUpdate['ALID'];?>
  154. <? echo $row_rsUpdate['LinkName'] ?></td>
  155. <td width="102">
  156.  
  157. <?php
  158. //find the user's assigned links
  159. $rowlink=$row_rsUpdate['LinkID'];
  160. mysql_select_db($database_mambo, $mambo);
  161. $query_rsAssignedLinks = "SELECT * FROM assignedlinks WHERE username = '$username' and ALID = '$rowlink' and `Add` ='Yes'";
  162. $rsAssignedLinks = mysql_query($query_rsAssignedLinks, $mambo) or die(mysql_error());
  163. $row_rsAssignedLinks = mysql_fetch_assoc($rsAssignedLinks);
  164. $totalRows_rsAssignedLinks = mysql_num_rows($rsAssignedLinks);
  165. $var= $row_rsAssignedLinks['Add'];
  166. if ($var ==="Yes"){
  167. $query_rsALFind = "UPDATE assignedlinks SET `Add`= 'No' WHERE `mambo_user_id` = '$id' AND `username` = '$username' AND ALID = '$rowlink'";
  168. $rsALFind = mysql_query($query_rsALFind, $mambo) or die(mysql_error());
  169. }
  170.  
  171. ?>
  172. <input type="checkbox" name="Add[]" value="<? echo $row_rsUpdate['LinkID']; ?>"
  173. <?php if ($_POST && (isset($_POST['Add'])=="1")) { echo "checked=\"checked\"";} ?> <?php $OK = isset($_POST['Add']) ? true : false; if($OK && $_POST['Add']=="Yes") { ?> <? } ?>/>
  174. Add
  175. <? echo $rowlink; ?>
  176.  
  177.  
  178. </td>
  179. <td width="92">
  180. <?php
  181. //find the user's assigned links
  182. $rowlink=$row_rsUpdate['LinkID'];
  183. mysql_select_db($database_mambo, $mambo);
  184. $query_rsAssignedLinks = "SELECT * FROM assignedlinks WHERE username = '$username' and ALID = '$rowlink' and `Update` ='Yes'";
  185. $rsAssignedLinks = mysql_query($query_rsAssignedLinks, $mambo) or die(mysql_error());
  186. $row_rsAssignedLinks = mysql_fetch_assoc($rsAssignedLinks);
  187. $totalRows_rsAssignedLinks = mysql_num_rows($rsAssignedLinks);
  188. $varUpdate= $row_rsAssignedLinks['Update'];
  189. if ($varUpdate ==="Yes"){
  190. $query_rsALFind = "UPDATE assignedlinks SET `Update`= 'No' WHERE `mambo_user_id` = '$id' AND `username` = '$username' AND ALID = '$rowlink'";
  191. $rsALFind = mysql_query($query_rsALFind, $mambo) or die(mysql_error());
  192. }
  193.  
  194.  
  195.  
  196. ?>
  197. <input type="checkbox" name="Update[]" value="<? echo $row_rsUpdate['LinkID']; ?>"
  198. <?php if ($_POST && (isset($_POST['Update'])=="1")) { echo "checked=\"checked\"";} ?> <?php $OK = isset($_POST['Update']) ? true : false; if($OK && $_POST['Update']=="Yes") { ?> <? } ?>/>
  199. Update</td>
  200. <td width="86">
  201. <?php
  202. //find the user's assigned links
  203. $rowlink=$row_rsUpdate['LinkID'];
  204. mysql_select_db($database_mambo, $mambo);
  205. $query_rsAssignedLinks = "SELECT * FROM assignedlinks WHERE username = '$username' and ALID = '$rowlink' and `Delete` ='Yes'";
  206. $rsAssignedLinks = mysql_query($query_rsAssignedLinks, $mambo) or die(mysql_error());
  207. $row_rsAssignedLinks = mysql_fetch_assoc($rsAssignedLinks);
  208. $totalRows_rsAssignedLinks = mysql_num_rows($rsAssignedLinks);
  209. $varDelete= $row_rsAssignedLinks['Delete'];
  210. if ($varDelete ==="Yes"){
  211. $query_rsALFind = "UPDATE assignedlinks SET `Delete`= 'No' WHERE `mambo_user_id` = '$id' AND `username` = '$username' AND ALID = '$rowlink'";
  212. $rsALFind = mysql_query($query_rsALFind, $mambo) or die(mysql_error());
  213. }
  214.  
  215.  
  216. ?>
  217. <input type="checkbox" name="Delete[]" value="<? echo $row_rsUpdate['LinkID']; ?>"
  218. <?php if ($_POST && (isset($_POST['Delete'])=="1")) { echo "checked=\"checked\"";} ?> <?php $OK = isset($_POST['Delete']) ? true : false; if($OK && $_POST['Delete']=="Yes") { ?> <? } ?>/>
  219.  
  220. Delete</td>
  221. <td width="80">
  222. <?php
  223. //find the user's assigned links
  224. $rowlink=$row_rsUpdate['LinkID'];
  225. mysql_select_db($database_mambo, $mambo);
  226. $query_rsAssignedLinks = "SELECT * FROM assignedlinks WHERE username = '$username' and ALID = '$rowlink' and `View` ='Yes'";
  227. $rsAssignedLinks = mysql_query($query_rsAssignedLinks, $mambo) or die(mysql_error());
  228. $row_rsAssignedLinks = mysql_fetch_assoc($rsAssignedLinks);
  229. $totalRows_rsAssignedLinks = mysql_num_rows($rsAssignedLinks);
  230. $varView= $row_rsAssignedLinks['View'];
  231. if ($varView ==="Yes"){
  232. $query_rsALFind = "UPDATE assignedlinks SET `View`= 'No' WHERE `mambo_user_id` = '$id' AND `username` = '$username' AND ALID = '$rowlink'";
  233. $rsALFind = mysql_query($query_rsALFind, $mambo) or die(mysql_error());
  234. }
  235.  
  236. ?>
  237. <input type="checkbox" name="View[]" value="<? echo $row_rsUpdate['LinkID']; ?>"
  238. <?php if ($_POST && (isset($_POST['View'])=="1")) { echo "checked=\"checked\"";} ?> <?php $OK = isset($_POST['View']) ? true : false; if($OK && $_POST['View']=="Yes") { ?> <? } ?>/>View</td>
  239. </tr>
  240. <?php } while ($row_rsUpdate = mysql_fetch_assoc($rsUpdate)); ?>
  241. <tr>
  242. <th colspan="6" scope="row"><input type="Submit" name="Submit" value="Submit" /></th>
  243. </tr>
  244. </table>
  245. <input type="hidden" name="MM_Update" value="update">
  246. </form>
  247. <? } ?>
  248. </body>
  249. </html>
  250. <?php
  251. mysql_free_result($rsUser);
  252. mysql_free_result($rsUpdate);
  253. ?>
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
JeniF is offline Offline
52 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP tags into HTML tags
Next Thread in PHP Forum Timeline: Not getting information from the database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC