| | |
PHP Variables through Checkboxes
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Feb 2005
Posts: 10
Reputation:
Solved Threads: 0
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:
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
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)
<form method="POST" name="myform2" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <br> Please enter a UserID to update with award icons:<br> UserID: <input type="text" name="multiid"> <br> <br> Attack <input type="checkbox" onClick="document.myform2.a0.value = this.checked">< Defence <input type="checkbox" onClick="document.myform2.b0.value = this.checked"><br> <input type="hidden" name="b0" value="3"> Strength <input type="checkbox" onClick="document.myform2.c0.value = this.checked"><br> <input type="hidden" name="c0" value="13"> HP <input type="checkbox" onClick="document.myform2.d0.value = this.checked"> <br> <input type="hidden" name="d0" value="11"> Ranged <input type="checkbox" onClick="document.myform2.e0.value = this.checked"> <br> <input type="hidden" name="e0" value="16"> Prayer <input type="checkbox" onClick="document.myform2.f0.value = this.checked"> <br> <input type="hidden" name="f0" value="19"> Magic <input type="checkbox" onClick="document.myform2.g0.value = this.checked"> <br> <input type="hidden" name="g0" value="12"> Cooking <input type="checkbox" onClick="document.myform2.h0.value = this.checked"> <br> <input type="hidden" name="h0" value="1"> Woodcutting <input type="checkbox" onClick="document.myform2.i0.value = this.checked"> <br> <input type="hidden" name="i0" value="21"> Fletching <input type="checkbox" onClick="document.myform2.j0.value = this.checked"> <br> <input type="hidden" name="j0" value="4"> Fishing <input type="checkbox" onClick="document.myform2.k0.value = this.checked"> <br> <input type="hidden" name="k0" value="9"> Firemaking <input type="checkbox" onClick="document.myform2.l0.value = this.checked"> <br> <input type="hidden" name="l0" value="8"> Crafting <input type="checkbox" onClick="document.myform2.m0.value = this.checked"> <br> <input type="hidden" name="m0" value="6"> Smithing <input type="checkbox" onClick="document.myform2.n0.value = this.checked"> <br> <input type="hidden" name="n0" value="15"> Mining <input type="checkbox" onClick="document.myform2.o0.value = this.checked"> <br> <input type="hidden" name="o0" value="14"> Herblore <input type="checkbox" onClick="document.myform2.p0.value = this.checked"> <br> <input type="hidden" name="p0" value="10"> Agility <input type="checkbox" onClick="document.myform2.q0.value = this.checked"> <br> <input type="hidden" name="q0" value="5"> Thieving <input type="checkbox" onClick="document.myform2.r0.value = this.checked"> <br> <input type="hidden" name="r0" value="20"> Slayer <input type="checkbox" onClick="document.myform2.s0.value = this.checked"> <br> <input type="hidden" name="s0" value="18"> Farming <input type="checkbox" onClick="document.myform2.t0.value = this.checked"> <br> <input type="hidden" name="t0" value="7"> Runecraft <input type="checkbox" onClick="document.myform2.u0.value = this.checked"> <br> <input type="hidden" name="u0" value="17"> Hunter <input type="checkbox" onClick="document.myform2.v0.value = this.checked"> <br> <input type="hidden" name="v0" value="47"> Construction <input type="checkbox" onClick="document.myform2.w0.value = this.checked"> <br> <input type="hidden" name="w0" value="50"> <br><br> <input type="submit" name="Submit"> </form> <? $attack80 = $_POST['a0']; $defence80 = $_POST['b0']; $strength80 = $_POST['c0']; $hp80 = $_POST['d0']; $ranged80 = $_POST['e0']; $prayer80 = $_POST['f0']; $magic80 = $_POST['g0']; $cooking80 = $_POST['h0']; $woodcutting80 = $_POST['i0']; $fletching80 = $_POST['j0']; $fishing80 = $_POST['k0']; $firemaking80 = $_POST['l0']; $crafting80 = $_POST['m0']; $smithing80 = $_POST['n0']; $mining80 = $_POST['o0']; $herblore80 = $_POST['p0']; $agility80 = $_POST['q0']; $thieving80 = $_POST['r0']; $slayer80 = $_POST['s0']; $farming80 = $_POST['t0']; $runecraft80 = $_POST['u0']; $hunter80 = $_POST['v0']; $construction80 = $_POST['w0']; $multiid = $_POST['multiid']; if ($multiid = '') { echo "Please enter an userid"; } else { if ($attack80 = '2') { echo "Yes this works"; $db->query_write(" INSERT INTO " . TABLE_PREFIX . "award_user (award_id, userid, issue_reason, issue_time) VALUES ('2', '$multiid', '" . addslashes($vbulletin->GPC['issue_reason']) . "', " . time() . ") "); } if ($defence80 = '3') { $db->query_write(" INSERT INTO " . TABLE_PREFIX . "award_user (award_id, userid, issue_reason, issue_time) VALUES ('3', '$multiid', '" . addslashes($vbulletin->GPC['issue_reason']) . "', " . time() . ") "); } }
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.
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:
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
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
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)
<input type="checkbox" onClick="document.myform2.w0.value = this.checked"> <input type="hidden" name="w0" value="50">
Why don't you use just
html Syntax (Toggle Plain Text)
<input type="checkbox" name="w0" value="50">
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.
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.
•
•
Join Date: Feb 2005
Posts: 10
Reputation:
Solved Threads: 0
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
Thanks very much
Problem solved and positive rep
![]() |
Similar Threads
- passing checkboxes to php using array in form (PHP)
- check box form (PHP)
- checkboxes (PHP)
- URL parsing to get variables name (PHP)
- PHP Checkbox (PHP)
- Problem Unsetting Variables? (PHP)
Other Threads in the PHP Forum
- Previous Thread: mail function not allowed
- Next Thread: open popup without using function
| Thread Tools | Search this Thread |
# 5.2.10 ajax apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date display dissertation dynamic echo echo$_get[x]changingitintovariable... email error file files folder form forms function functions google href htaccess html image images include insert integration ip java javascript joomla ldap legislation limit link local login loop mail memberships menu mlm multiple multipletables mysql mysqlquery oop open paypal pdf persist php problem query radio random recursion regex remote rss script search server sessions sms soap sockets source space spam sql syntax system table tutorial update upload url validator variable video web xml youtube





