PHP Variables through Checkboxes

Thread Solved

Join Date: Feb 2005
Posts: 10
Reputation: JayJ is an unknown quantity at this point 
Solved Threads: 0
JayJ JayJ is offline Offline
Newbie Poster

PHP Variables through Checkboxes

 
0
  #1
Apr 2nd, 2008
Hi all,

Basically I'm banging my head against the wall recently trying to get a list of checkboxes to pass variables to a SQL query. If the checkbox is ticked then I wish for the a certain SQL query to be passed. This is my code at the moment that I'm toying with:

  1. <form method="POST" name="myform2" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  2. <br>
  3. Please enter a UserID to update with award icons:<br>
  4. UserID: <input type="text" name="multiid"> <br>
  5. <br>
  6. Attack <input type="checkbox" onClick="document.myform2.a0.value = this.checked"><
  7. Defence <input type="checkbox" onClick="document.myform2.b0.value = this.checked"><br>
  8. <input type="hidden" name="b0" value="3">
  9. Strength <input type="checkbox" onClick="document.myform2.c0.value = this.checked"><br>
  10. <input type="hidden" name="c0" value="13">
  11. HP <input type="checkbox" onClick="document.myform2.d0.value = this.checked"> <br>
  12. <input type="hidden" name="d0" value="11">
  13. Ranged <input type="checkbox" onClick="document.myform2.e0.value = this.checked"> <br>
  14. <input type="hidden" name="e0" value="16">
  15. Prayer <input type="checkbox" onClick="document.myform2.f0.value = this.checked"> <br>
  16. <input type="hidden" name="f0" value="19">
  17. Magic <input type="checkbox" onClick="document.myform2.g0.value = this.checked"> <br>
  18. <input type="hidden" name="g0" value="12">
  19. Cooking <input type="checkbox" onClick="document.myform2.h0.value = this.checked"> <br>
  20. <input type="hidden" name="h0" value="1">
  21. Woodcutting <input type="checkbox" onClick="document.myform2.i0.value = this.checked"> <br>
  22. <input type="hidden" name="i0" value="21">
  23. Fletching <input type="checkbox" onClick="document.myform2.j0.value = this.checked"> <br>
  24. <input type="hidden" name="j0" value="4">
  25. Fishing <input type="checkbox" onClick="document.myform2.k0.value = this.checked"> <br>
  26. <input type="hidden" name="k0" value="9">
  27. Firemaking <input type="checkbox" onClick="document.myform2.l0.value = this.checked"> <br>
  28. <input type="hidden" name="l0" value="8">
  29. Crafting <input type="checkbox" onClick="document.myform2.m0.value = this.checked"> <br>
  30. <input type="hidden" name="m0" value="6">
  31. Smithing <input type="checkbox" onClick="document.myform2.n0.value = this.checked"> <br>
  32. <input type="hidden" name="n0" value="15">
  33. Mining <input type="checkbox" onClick="document.myform2.o0.value = this.checked"> <br>
  34. <input type="hidden" name="o0" value="14">
  35. Herblore <input type="checkbox" onClick="document.myform2.p0.value = this.checked"> <br>
  36. <input type="hidden" name="p0" value="10">
  37. Agility <input type="checkbox" onClick="document.myform2.q0.value = this.checked"> <br>
  38. <input type="hidden" name="q0" value="5">
  39. Thieving <input type="checkbox" onClick="document.myform2.r0.value = this.checked"> <br>
  40. <input type="hidden" name="r0" value="20">
  41. Slayer <input type="checkbox" onClick="document.myform2.s0.value = this.checked"> <br>
  42. <input type="hidden" name="s0" value="18">
  43. Farming <input type="checkbox" onClick="document.myform2.t0.value = this.checked"> <br>
  44. <input type="hidden" name="t0" value="7">
  45. Runecraft <input type="checkbox" onClick="document.myform2.u0.value = this.checked"> <br>
  46. <input type="hidden" name="u0" value="17">
  47. Hunter <input type="checkbox" onClick="document.myform2.v0.value = this.checked"> <br>
  48. <input type="hidden" name="v0" value="47">
  49. Construction <input type="checkbox" onClick="document.myform2.w0.value = this.checked"> <br>
  50. <input type="hidden" name="w0" value="50">
  51. <br><br>
  52. <input type="submit" name="Submit"> </form>
  53.  
  54.  
  55. <?
  56. $attack80 = $_POST['a0'];
  57. $defence80 = $_POST['b0'];
  58. $strength80 = $_POST['c0'];
  59. $hp80 = $_POST['d0'];
  60. $ranged80 = $_POST['e0'];
  61. $prayer80 = $_POST['f0'];
  62. $magic80 = $_POST['g0'];
  63. $cooking80 = $_POST['h0'];
  64. $woodcutting80 = $_POST['i0'];
  65. $fletching80 = $_POST['j0'];
  66. $fishing80 = $_POST['k0'];
  67. $firemaking80 = $_POST['l0'];
  68. $crafting80 = $_POST['m0'];
  69. $smithing80 = $_POST['n0'];
  70. $mining80 = $_POST['o0'];
  71. $herblore80 = $_POST['p0'];
  72. $agility80 = $_POST['q0'];
  73. $thieving80 = $_POST['r0'];
  74. $slayer80 = $_POST['s0'];
  75. $farming80 = $_POST['t0'];
  76. $runecraft80 = $_POST['u0'];
  77. $hunter80 = $_POST['v0'];
  78. $construction80 = $_POST['w0'];
  79.  
  80. $multiid = $_POST['multiid'];
  81.  
  82. if ($multiid = '')
  83. {
  84. echo "Please enter an userid";
  85. }
  86.  
  87.  
  88. else
  89. {
  90.  
  91. if ($attack80 = '2')
  92. {
  93. echo "Yes this works";
  94. $db->query_write("
  95. INSERT INTO " . TABLE_PREFIX . "award_user
  96. (award_id, userid, issue_reason, issue_time)
  97. VALUES ('2', '$multiid', '" . addslashes($vbulletin->GPC['issue_reason']) . "', " . time() . ")
  98. ");
  99. }
  100.  
  101. if ($defence80 = '3')
  102. {
  103. $db->query_write("
  104. INSERT INTO " . TABLE_PREFIX . "award_user
  105. (award_id, userid, issue_reason, issue_time)
  106. VALUES ('3', '$multiid', '" . addslashes($vbulletin->GPC['issue_reason']) . "', " . time() . ")
  107. ");
  108. }
  109.  
  110. }

Any help or assistance would be greatly appreciated with this as I have been pulling my hair out for the past week over this and different tutorials are not easy to comprehend with regards to checkbox values.

Kind Regards


JayJ

p.s. Good to see Daniweb going strong after all these years
Last edited by JayJ; Apr 2nd, 2008 at 6:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 100
Reputation: petr.pavel is an unknown quantity at this point 
Solved Threads: 14
petr.pavel's Avatar
petr.pavel petr.pavel is offline Offline
Junior Poster

Re: PHP Variables through Checkboxes

 
0
  #2
Apr 2nd, 2008
Hi there, could you please let us know what isn't working? I.e. what is the behaviour you expect and what is it doing instead.

BTW: Are you sure you need all that JavaScript hassle with onClick on the checkboxes?

Maybe you don't know what is being submitted. For example here:
  1. <input type="checkbox" onClick="document.myform2.w0.value = this.checked">
  2. <input type="hidden" name="w0" value="50">
If clicked w0 will be assinged "true". Otherwise it will keep "50". Also if I click the checkbox and then click it again (to uncheck it), w0 will still hold "true".

Why don't you use just
  1. <input type="checkbox" name="w0" value="50">
If checked w0 will contain "50". If unchecked then w0 will not be submitted - use if (isset($_POST['w0'])) in your script to test it.

To see what your script is getting put var_dump($_POST); somewhere into your script.
Petr 'PePa' Pavel

The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 10
Reputation: JayJ is an unknown quantity at this point 
Solved Threads: 0
JayJ JayJ is offline Offline
Newbie Poster

Re: PHP Variables through Checkboxes

 
0
  #3
Apr 2nd, 2008
You are quite correct - I overcomplicated it attempting to use Javascript which is outside of my comfort zone. I had thought that something as simple would work, but didn't try it having searched and been confronted with lots of confusing tutorials that had javascript examples.

Thanks very much Problem solved and positive rep
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC