snookian Posted March 20, 2013 Share Posted March 20, 2013 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 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 Quote Link to comment https://forums.phpfreaks.com/topic/275931-creating-and-storing-array-of-check-boxs/ Share on other sites More sharing options...
PaulRyan Posted March 20, 2013 Share Posted March 20, 2013 You need to put the checkboxes into an array, like this: name="invoice_details[]" Then when the form is posted, check the posted check boxes and save the data Quote Link to comment https://forums.phpfreaks.com/topic/275931-creating-and-storing-array-of-check-boxs/#findComment-1419871 Share on other sites More sharing options...
snookian Posted March 21, 2013 Author Share Posted March 21, 2013 You need to put the checkboxes into an array, like this: name="invoice_details[]" Then when the form is posted, check the posted check boxes and save the data I have tried this but it just saves as "Array" in my database. Ian Quote Link to comment https://forums.phpfreaks.com/topic/275931-creating-and-storing-array-of-check-boxs/#findComment-1420086 Share on other sites More sharing options...
haku Posted March 21, 2013 Share Posted March 21, 2013 You have to parse the submitted data before saving it to the database. Quote Link to comment https://forums.phpfreaks.com/topic/275931-creating-and-storing-array-of-check-boxs/#findComment-1420088 Share on other sites More sharing options...
snookian Posted March 21, 2013 Author Share Posted March 21, 2013 You have to parse the submitted data before saving it to the database. As in ? parse_str($invoice_details); I'm having a real mind blank, seems to happen all too often lol. (I'm also not to proud to admit I'm a bit of a PHP noob) Ian Quote Link to comment https://forums.phpfreaks.com/topic/275931-creating-and-storing-array-of-check-boxs/#findComment-1420093 Share on other sites More sharing options...
haku Posted March 21, 2013 Share Posted March 21, 2013 (edited) The submitted values are an array. You have saved the array directly to the database, but databases (or at least MySQL, can't speak for others) can't store PHP arrays, they can only store strings. So you are going to have to parse that array, and turn it into a string. You can use serialize() if you want. But then the indivudual values will not be searchable, which isn't the best database practice. Edited March 21, 2013 by haku Quote Link to comment https://forums.phpfreaks.com/topic/275931-creating-and-storing-array-of-check-boxs/#findComment-1420096 Share on other sites More sharing options...
PaulRyan Posted March 21, 2013 Share Posted March 21, 2013 I did say "check the posted check boxes", this means outputting the values, to make sure they are what you expect, and work from that. When the form is posted, BEFORE you insert it into the database, use var_dump to check the values. Quote Link to comment https://forums.phpfreaks.com/topic/275931-creating-and-storing-array-of-check-boxs/#findComment-1420105 Share on other sites More sharing options...
snookian Posted March 21, 2013 Author Share Posted March 21, 2013 I really appreciate everyones help, but im still struggling, PaulRyan I have done what you said, and used Var_dump like this var_dump($invoice_details[2]); To check that the value in that slot is what i want, and it is, but i having major problems storing to the database, i understand what haku is getting at about converting the array to a string before input but I have no idea where/how to do this. Ian Quote Link to comment https://forums.phpfreaks.com/topic/275931-creating-and-storing-array-of-check-boxs/#findComment-1420122 Share on other sites More sharing options...
snookian Posted March 21, 2013 Author Share Posted March 21, 2013 I have changed my code to the following <?php $i=0; foreach ( $results['detailsline'] as $detailsline ){ $invoice_details[] = $detailsline->details_line; echo $invoice_details[$i]; echo '<input type="checkbox" name="invoice_details[$i]" value="'.$invoice_details.'"/>'; $i++; } var_dump($invoice_details[4]); ?> This works well creating the layout (echo or each choice) and the vardump bit reads the values it should. I have tried using implode to create a string like below, but as i said and last post im at a loss if this is right ot where to put it. I think my naming conventions may be confussing me too, the field in the databse where this value is stored is called invoice_details, but as you can see i have used it for most things relating. $idetails[] = implode(", ", $invoice_details); Ian Quote Link to comment https://forums.phpfreaks.com/topic/275931-creating-and-storing-array-of-check-boxs/#findComment-1420127 Share on other sites More sharing options...
Solution snookian Posted March 21, 2013 Author Solution Share Posted March 21, 2013 Thank you all for your help, I think i have solved it.Ian Quote Link to comment https://forums.phpfreaks.com/topic/275931-creating-and-storing-array-of-check-boxs/#findComment-1420143 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.