SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
Thread: Setting Checkbox Variables
-
Apr 25, 2006, 07:09 #1
- Join Date
- Apr 2004
- Location
- dublin
- Posts
- 2,036
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Setting Checkbox Variables
Hi all,
I have many checkboxes like below. I want to set variables for each users selection and insert that value into DB.
How do I set the variables?
Thanks.
PHP Code:<tr>
<td>Equities</td>
<td><input type="checkbox" name="equities[close]" id="equities[close]" value="close" /></td>
<td><input type="checkbox" name="equities[bid]" id="equities[bid]" value="bid" /></td>
<td><input type="checkbox" name="equities[ask]" id="equities[ask]" value="ask" /></td>
<td><input type="checkbox" name="equities[mean]" id="equities[mean]" value="mean" /></td>
<td><input type="checkbox" name="equities[manual]" id="equities[manual]" value="manual" /></td>
<td><textarea rows="3" cols="15"></textarea></td>
</tr>
<tr>
<td>Bonds</td>
<td><input type="checkbox" name="bonds[close]" id="bonds[close]" value="close" /></td>
<td><input type="checkbox" name="bonds[bid]" id="bonds[bid]" value="bid" /></td>
<td><input type="checkbox" name="bonds[ask]" id="bonds[ask]" value="ask" /></td>
<td><input type="checkbox" name="bonds[mean]" id="bonds[mean]" value="mean" /></td>
<td><input type="checkbox" name="bonds[manual]" id="bonds[manual]" value="manual" /></td>
</tr>
-
Apr 25, 2006, 07:14 #2
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
PHP will populate two associative arrays for you:
Code:$_POST Array ( [equities] => Array ( [close] => close [bid] => bid [ask] => ask [mean] => mean [manual] => manual ) [bonds] => Array ( [close] => close [bid] => bid [ask] => ask [mean] => mean [manual] => manual ) )
Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
Apr 25, 2006, 07:20 #3
Originally Posted by Dan Grossman
-
Apr 25, 2006, 07:24 #4
- Join Date
- Apr 2004
- Location
- dublin
- Posts
- 2,036
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
sorry head not right today.
how do I use those arrays?? To insert into database?
thanks.
-
Apr 25, 2006, 07:33 #5
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:$close = isset($_POST['equities']['close']) ? 1 : 0;
Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
Apr 25, 2006, 07:44 #6
- Join Date
- Apr 2004
- Location
- dublin
- Posts
- 2,036
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
So do I have to repeat that for each array item?
-
Apr 25, 2006, 07:46 #7
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Since you need to get a value for each one to insert into your database, yes. Simply looping over the array won't work since there won't be entries for the checkboxes that weren't checked.
That code will assign $close 1 if the equities[close] checkbox was checked, 0 if it wasn't (and thus isn't in the array at all). You can change those values to whatever you want to put in your database when a checkbox is checked or unchecked.Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
Apr 25, 2006, 07:51 #8
- Join Date
- Apr 2004
- Location
- dublin
- Posts
- 2,036
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
OK. Thanks. Do you spot a syntax error somewhere? Get a blank page now when add the below code...
PHP Code:// Check Pricing Variables
$_POST Array
(
[equities] => Array
(
[close] => close
[bid] => bid
[ask] => ask
[mean] => mean
[manual] => manual
)
[bonds] => Array
(
[close] => close
[bid] => bid
[ask] => ask
[mean] => mean
[manual] => manual
)
[futures] => Array
(
[close] => close
[bid] => bid
[ask] => ask
[mean] => mean
[manual] => manual
)
[options] => Array
(
[close] => close
[bid] => bid
[ask] => ask
[mean] => mean
[manual] => manual
)
)
$equity_close = isset($_POST['equities']['close']) ? 1 : 0;
$equity_bid = isset($_POST['equities']['bid']) ? 1 : 0;
$equity_ask = isset($_POST['equities']['ask']) ? 1 : 0;
$equity_mean = isset($_POST['equities']['mean']) ? 1 : 0;
$equity_manual = isset($_POST['equities']['manual']) ? 1 : 0;
-
Apr 25, 2006, 08:12 #9
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
You don't want to add that Array ( ) stuff up top. That was just me showing you what arrays were created from posting your form. That's not actual code.
Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
Apr 25, 2006, 08:43 #10
- Join Date
- Apr 2004
- Location
- dublin
- Posts
- 2,036
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ok. Thanks.
Bookmarks