SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard
    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

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    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
            )
    
    )
    Only checkboxes that were checked will be present in the array -- unchecked boxes will not have keys/indexes at all.

  3. #3
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman
    Only checkboxes that were checked will be present in the array -- unchecked boxes will not have keys/indexes at all.
    ... which might explain the problem you're having here...

  4. #4
    SitePoint Wizard
    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.

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    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']) ? 0

  6. #6
    SitePoint Wizard
    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?

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    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.

  8. #8
    SitePoint Wizard
    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']) ? 0;
     
    $equity_bid = isset($_POST['equities']['bid']) ? 0;
     
    $equity_ask = isset($_POST['equities']['ask']) ? 0;
     
    $equity_mean = isset($_POST['equities']['mean']) ? 0;
     
    $equity_manual = isset($_POST['equities']['manual']) ? 0

  9. #9
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    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.

  10. #10
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok. Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •