fesan Posted March 13, 2009 Share Posted March 13, 2009 Hello.... I have a form with many check boxes. I want to make a variable that includes all the info from just the selected ones. so if my check boxes returns values 1 and 2 and 3 i want my variable to have the value 1, 2, 3. The check boxes are for checking what fields in the database to include in a group. and my variable is supposed to be the one that chooses the fields with mysql command: SELECT $variable from $dbtable Thank you for answers! Quote Link to comment https://forums.phpfreaks.com/topic/149289-retrieve-only-checked-check-boxes-from-form/ Share on other sites More sharing options...
rhodesa Posted March 13, 2009 Share Posted March 13, 2009 <input type="checkbox" name="chkbx[]" value="1" /> <input type="checkbox" name="chkbx[]" value="2" /> <input type="checkbox" name="chkbx[]" value="3" /> <input type="checkbox" name="chkbx[]" value="4" /> <input type="checkbox" name="chkbx[]" value="5" /> $checked = implode(', ',$_POST['chkbx']); Quote Link to comment https://forums.phpfreaks.com/topic/149289-retrieve-only-checked-check-boxes-from-form/#findComment-784012 Share on other sites More sharing options...
fesan Posted March 14, 2009 Author Share Posted March 14, 2009 Thank you for the reply. But i cant get it to work.... I get the error: Warning: implode() [function.implode]: Invalid arguments passed in my folder and line My code looks like this(cutted out the rest of the form and irrelevant PHP): $list_include = implode(', ',$_POST['checkbox']); echo $list_include; <form name="form1" method="post" action="<?php echo $PHP_SELF;?>"> <?php include("conn/conn_list.php"); $query = "SHOW COLUMNS FROM $dbtable"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo "<label> <input type='checkbox' name='checkbox' value='". $row['0'] ."' id='". $row['0'] ."'> ". $row['0'] ."</label> <br>"; } ?> </form> Quote Link to comment https://forums.phpfreaks.com/topic/149289-retrieve-only-checked-check-boxes-from-form/#findComment-784476 Share on other sites More sharing options...
Ayon Posted March 14, 2009 Share Posted March 14, 2009 <input type='checkbox' name='checkbox[]' value='". $row['0'] ."' id='". $row['0'] ."'> Quote Link to comment https://forums.phpfreaks.com/topic/149289-retrieve-only-checked-check-boxes-from-form/#findComment-784477 Share on other sites More sharing options...
fesan Posted March 14, 2009 Author Share Posted March 14, 2009 Yea i tried with the [] too but still the same error.... Quote Link to comment https://forums.phpfreaks.com/topic/149289-retrieve-only-checked-check-boxes-from-form/#findComment-784479 Share on other sites More sharing options...
fesan Posted March 14, 2009 Author Share Posted March 14, 2009 Sorry looks like I've missed something because now it works! Thanks for the Help! Quote Link to comment https://forums.phpfreaks.com/topic/149289-retrieve-only-checked-check-boxes-from-form/#findComment-784481 Share on other sites More sharing options...
rhodesa Posted March 14, 2009 Share Posted March 14, 2009 you will get that Warning when the page loads the first time because there is no POST data until the form is submitted. put that first line with the implode inside an if that checks to see if the form has been submitted Quote Link to comment https://forums.phpfreaks.com/topic/149289-retrieve-only-checked-check-boxes-from-form/#findComment-784571 Share on other sites More sharing options...
fesan Posted March 15, 2009 Author Share Posted March 15, 2009 Yea.. i figured that out.. And have solved the problem with an if that checks if the form is submitted. Thanks.... Have a new issue though. After i have imploded the values to a one variable i want to explode them to separate variables again. Got the explode to work and get the correct output. But by the output of the explode i want to include different form fields. like: $list_include = explode(', ',$row['list_include']); if($list_include[0] = 'product'){ include("source/form_product.php"); } And so on... The number of arrays in $list_include can maximum be 28, but i expect it to extend... How can i in a easy way use all of the different arrays and include a form by the output? My intention with the whole function from the beginning is to make a group where the products are a member of. The group selects what fields in the database to use. And now I'm at the part where want to put out just the fields that is selected by the group. I get the fields out in the array $list_include. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/149289-retrieve-only-checked-check-boxes-from-form/#findComment-785187 Share on other sites More sharing options...
rhodesa Posted March 16, 2009 Share Posted March 16, 2009 first of all, in the case, if you are trying to store an array of data into a table, i would use serialize() and unserialize()....that way the values could have a comma in the them and it wouldn't break the code on to your code. first, you want a double equals in your IF statement, not a single one. next, the includes would depend on what you are trying to do. is there an include for each 'keyword'? if so, just keep an array mapping the keywords to the includes, then loop over the $list_include and do the include. or, if it's always form_<keyword>.php you could just do: $list_include = explode(', ',$row['list_include']); foreach($list_inlclude as $include){ include("source/form_{$include}.php"); } if it's only for certain keywords, you can just test for those with an IF and in_array() Quote Link to comment https://forums.phpfreaks.com/topic/149289-retrieve-only-checked-check-boxes-from-form/#findComment-785622 Share on other sites More sharing options...
exally Posted March 16, 2009 Share Posted March 16, 2009 if($list_include[0] = 'product'){ include("source/form_product.php"); } to if($list_include[0] == 'product'){ include("source/form_product.php"); } Quote Link to comment https://forums.phpfreaks.com/topic/149289-retrieve-only-checked-check-boxes-from-form/#findComment-785650 Share on other sites More sharing options...
fesan Posted March 16, 2009 Author Share Posted March 16, 2009 Thanks you guys.... I will try this one out! And i like if with one =, It makes it a bit loose in the edges.. Quote Link to comment https://forums.phpfreaks.com/topic/149289-retrieve-only-checked-check-boxes-from-form/#findComment-786084 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.