www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Posting array from checkboxs

  1. #1
    Join Date
    Apr 2011
    Posts
    6

    resolved [RESOLVED] Posting array from checkboxs

    Hi everyone, I have an issue posting an array from check boxs, simply put i have a database that contains some details, i want to take these details, split them into an array and therefore separate check boxs then a user will select a number of check box and then this will be saved as an array into a seperate table, hope i explained that ok.

    So here is the code I have some far, I have some code elsewhere for the $result part, and for the submit part, but basically its grabs the information from a table and splits it into check boxs (in theory) the "label" part works, i have this here to check that its is getting the right information and it is, the problem happens when I submit the form, it will only save the last checked check box, so if i click say check box 1,3,5 (there could be an unlimited amount of options which is why im pulling the data from another table) and these values are One, Three, Five only Five will save in the table.

    PHP Code:
    <?php foreach ( $results['detailsline'] as $detailsline ){
    $invoice_details $detailsline->details_line;
    echo 
    '<label>'.$invoice_details.'</label><input type="checkbox" name="invoice_details" value="'.$invoice_details.'"/>';   
     }                  
    ?>
    If I have explained this well enough can someone guide me please.

    Ian

  2. #2
    Join Date
    May 2003
    Posts
    599
    when your loop runs, it creates a bunch of inputs all with the same name (invoice_details). This means that when the data is posted, the value of the last input of that name will be used.

    When working with checkboxes, It is a good idea to post that data in its own array to keep track of all the values for example:

    <input type="checkbox" name="invoice_details[value_1]" value="'.$val.'"/>
    <input type="checkbox" name="invoice_details[value_2]" value="'.$val.'"/>

    etc...of course you can use any key names that make sense.

  3. #3
    Join Date
    Apr 2011
    Posts
    6
    Thanks for the reply, However I'm not sure how many values there will be (check box's), because on a previous screen the user will select some options which could result in the need from anywhere between two check box's or 30 for example. So i cant create a separate input field like that (or at least I don't think I can), because I don't know how many there will be on each occasion.

    Thanks

    Ian

  4. #4
    Join Date
    Mar 2013
    Location
    Iasi, Romania
    Posts
    53
    You could use some sort of an index...like the $i variable i used bellow

    <?php
    $i=1;
    foreach ( $results['detailsline'] as $detailsline ){
    $invoice_details = $detailsline->details_line;
    echo '<label>'.$invoice_details.'</label><input type="checkbox" name="invoice_details'.$i.'" value="'.$invoice_details.'"/>';
    $i++;
    }
    ?>

  5. #5
    Join Date
    Apr 2011
    Posts
    6
    I have used something very similar, and it works great for having the name of each array value next to the check box etc etc, but Im still having major trouble storing it into the DB.

    Here is my code:

    PHP Code:
            <?php
                $i
    =0;
                foreach ( 
    $results['detailsline'] as $details ){
                    
    $idetails[] = $details->details_line;            
                                    
                    echo 
    $idetails[$i];
                     echo 
    '<input type="checkbox" name="invoice_details[$i]" value="'.$idetails[$i].'"/>';  
                    
    $i++;
                  }        
                    
    var_dump($idetails[1]);        
            
    ?>
    But its stores into the DB as "Array", I need to convert to a string before submitting (I was told) but i have no idea how to do this.

    Thanks for your reply.
    Ian
    Last edited by snookian; 03-21-2013 at 08:21 AM. Reason: No formatting

  6. #6
    Join Date
    Mar 2013
    Location
    Iasi, Romania
    Posts
    53
    you could use implode to convert it to string, if that's the problem
    PHP Code:
    <?php
    $idetails
    [1]=implode("', '"$idetails[1]);
    ?>

  7. #7
    Join Date
    Apr 2011
    Posts
    6
    Thank you all for your help, I think i have solved it.

    Ian

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles