mikenl Posted August 19, 2008 Share Posted August 19, 2008 Hi, I generate a list of checkboxes like this: // here I set an array, $rows is db query result $language = array(1 => "Afrikaans","Albanian","Arabic","Armenian","Bangla","Bosnian","Bulgarian","Burmese","Catalan","Cebuano","Chinese","Croatian","Czech","Danish","Dutch","English","Esperanto","Estonian","Finnish","French","Gaelic","German","Greek","Hebrew","Hindi","Hungarian","Icelandic","Indonesian","Italian","Japanese","Korean","Kurdish","Latin","Latvian","Lithuanian","Luxembourgish","Macedonian","Malay","Norwegian","Persian","Polish","Portuguese","Romanian","Romansh","Russian","Serbian","Sign Language","Slovak","Slovenian","Sorbian","Spanish","Swedish","Swiss German","Tagalog","Thai","Turkish","Ukrainian","Welsh"); loadform2($language,$rows,'lang'); // here I create the checkbox list function loadform2($values,$rows,$fieldname){ if($_POST){ $output = $_POST; } else { $output = $rows; } $size = count($values); ?> <div style="width:125px;height:100px;overflow:auto;margin-bottom:7px; padding:0"> <? foreach ($values as $key => $value){ if(!empty($key)){ echo "<input id=\"$fieldname".$key."\" name=\"$fieldname"."[]"."\" type=\"checkbox\" value=\"$key\""; if($_POST){ // how do I make this work? $output is $_POST data if ($output[0][$fieldname][$key] == $key-1) echo "checked"; } else { // this works, $output comes from db query if ($output[0][$fieldname.$key] == 1) echo "checked"; } echo "/><label for=\"$fieldname".$key."\">$value</label><br />\n"; } } ?> <input name="<? echo $fieldname ?>_num" type="hidden" value="<? echo $size ?>" /> </div> <? } I want to have all checkboxes that were checked when POSTed to 'stick' but don't know how to do this for $_POST data... Anyone knows how to handle this? Link to comment https://forums.phpfreaks.com/topic/120357-checkbox-_post-remember/ Share on other sites More sharing options...
adam84 Posted August 19, 2008 Share Posted August 19, 2008 hmmm will if($_POST){ } ever evaluate to true? Link to comment https://forums.phpfreaks.com/topic/120357-checkbox-_post-remember/#findComment-620111 Share on other sites More sharing options...
mikenl Posted August 19, 2008 Author Share Posted August 19, 2008 yes here's a print_f of $_POST: [lang] => Array ( [0] => 1 [1] => 2 [2] => 58 ) boc 1, 2 and 58 are checked... Link to comment https://forums.phpfreaks.com/topic/120357-checkbox-_post-remember/#findComment-620120 Share on other sites More sharing options...
mikenl Posted August 19, 2008 Author Share Posted August 19, 2008 print_r of db query result: [lang1] => 1 [lang2] => 1 [lang3] => 0 [lang4] => 0 [lang5] => 0 [lang6] => 0 [lang7] => 0 [lang8] => 0 [lang9] => 0 [lang10] => 0 [lang11] => 0 [lang12] => 0 [lang13] => 0 [lang14] => 0 [lang15] => 0 [lang16] => 0 [lang17] => 0 [lang18] => 0 [lang19] => 0 [lang20] => 0 [lang21] => 0 [lang22] => 0 [lang23] => 0 [lang24] => 0 [lang25] => 0 [lang26] => 0 [lang27] => 0 [lang28] => 0 [lang29] => 0 [lang30] => 0 [lang31] => 0 [lang32] => 0 [lang33] => 0 [lang34] => 0 [lang35] => 0 [lang36] => 0 [lang37] => 0 [lang38] => 0 [lang39] => 0 [lang40] => 0 [lang41] => 0 [lang42] => 0 [lang43] => 0 [lang44] => 0 [lang45] => 0 [lang46] => 0 [lang47] => 0 [lang48] => 0 [lang49] => 0 [lang50] => 0 [lang51] => 0 [lang52] => 0 [lang53] => 0 [lang54] => 0 [lang55] => 0 [lang56] => 0 [lang57] => 0 [lang58] => 1 Link to comment https://forums.phpfreaks.com/topic/120357-checkbox-_post-remember/#findComment-620133 Share on other sites More sharing options...
mikenl Posted August 19, 2008 Author Share Posted August 19, 2008 print_r of $_POST (previous post has a o in it...): [lang] => Array ( [0] => 1 [1] => 2 [2] => 58 ) Link to comment https://forums.phpfreaks.com/topic/120357-checkbox-_post-remember/#findComment-620138 Share on other sites More sharing options...
mikenl Posted August 25, 2008 Author Share Posted August 25, 2008 Anyone? Link to comment https://forums.phpfreaks.com/topic/120357-checkbox-_post-remember/#findComment-624771 Share on other sites More sharing options...
mikenl Posted October 10, 2008 Author Share Posted October 10, 2008 Is there anyone on phpfreaks that can solve this? Link to comment https://forums.phpfreaks.com/topic/120357-checkbox-_post-remember/#findComment-661909 Share on other sites More sharing options...
xsist10 Posted October 10, 2008 Share Posted October 10, 2008 Here's a slight rework that should work <?php $options = array(1 => "Afrikaans","Albanian","Arabic","Armenian","Bangla","Bosnian","Bulgarian","Burmese","Catalan","Cebuano","Chinese","Croatian","Czech","Danish","Dutch","English","Esperanto","Estonian","Finnish","French","Gaelic","German","Greek","Hebrew","Hindi","Hungarian","Icelandic","Indonesian","Italian","Japanese","Korean","Kurdish","Latin","Latvian","Lithuanian","Luxembourgish","Macedonian","Malay","Norwegian","Persian","Polish","Portuguese","Romanian","Romansh","Russian","Serbian","Sign Language","Slovak","Slovenian","Sorbian","Spanish","Swedish","Swiss German","Tagalog","Thai","Turkish","Ukrainian","Welsh"); $selections = ($_POST['languages'] ? $_POST['languages'] : array()); function checkList($options, $selections, $name) { echo "<div style='width:500px;height:200px;overflow:auto;margin-bottom:7px; padding:0'>"; foreach ($options as $key => $value){ echo "<input id='". $name . $key ."' name='". $name ."[]' type='checkbox' value='". $key ."' ". (in_array($key, $selections) ? "checked" : "") .">"; echo "<label for='". $name . $key ."'>$value</label><br />\n"; } echo "</div>"; } ?> <form method='post'> <?php checkList($options, $selections, 'languages'); ?> <input type='submit'> </form> Link to comment https://forums.phpfreaks.com/topic/120357-checkbox-_post-remember/#findComment-661934 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.