943,608 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 6018
  • PHP RSS
Apr 2nd, 2008
0

PHP Variables through Checkboxes

Expand Post »
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:

php Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JayJ is offline Offline
17 posts
since Feb 2005
Apr 2nd, 2008
0

Re: PHP Variables through Checkboxes

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:
html Syntax (Toggle Plain Text)
  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
html Syntax (Toggle Plain Text)
  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.
Reputation Points: 27
Solved Threads: 16
Junior Poster
petr.pavel is offline Offline
116 posts
since Mar 2008
Apr 2nd, 2008
0

Re: PHP Variables through Checkboxes

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JayJ is offline Offline
17 posts
since Feb 2005

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: mail function not allowed
Next Thread in PHP Forum Timeline: Mysql to PHP





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


Follow us on Twitter


© 2011 DaniWeb® LLC