RLJ Posted December 10, 2010 Share Posted December 10, 2010 Hi, I use the following code to create a select menu from an array of options stored in LISTS.php: include 'LISTS.php'; print('<select id="from" name="from">'); foreach ($langList as $lang) {printf('<option %s>%s</option>', ($from1 == $lang ? 'selected="selected"' : ''), $lang); } echo '</select>'; where LISTS.php includes the following: $langList = array(' ','English', 'French', 'German', 'Dutch', 'Spanish'); This works great, but now I want to do something similar with a checkbox list, where each checkbox has an associated 'onchange' javascript function and I'm getting pretty stuck. My checkbox list is of the following form: <html> <ul style="height: 95px; overflow: auto; width: 200px; border: 1px solid #480091; list-style-type: none; margin: 0; padding: 0;"> <li id="li1b"><label for="chk1b"><input name="chk1b" id="chk1b" type="checkbox" onchange="function1('chk1b','li1b')">Option1</label></li> <li id="li2b"><label for="chk2b"><input name="chk2b" id="chk2b" type="checkbox" onchange="function1('chk2b','li2b')">Option2</label></li> //etc. </ul> </html> What I want to do is have 'Option1', 'Option2', etc. stored in an array in LISTS.php and have a PHP script that populates the checkbox list accordingly, in a similar manner to my select menu above. I can't work out how to get the ID of the next <li> and the next <input> in the list to go up by one each time, e.g. 'li1b' then 'li2b', 'li3b', etc. Could someone pls help me out? Thanks! Link to comment https://forums.phpfreaks.com/topic/221228-checkbox-list-with-list-options-stored-in-external-array/ Share on other sites More sharing options...
MMDE Posted December 10, 2010 Share Posted December 10, 2010 remember to put the inputs inside a form tag! <form method="post" action=""> <ul style="height: 95px; overflow: auto; width: 200px; border: 1px solid #480091; list-style-type: none; margin: 0; padding: 0;"> <li id="li1b"><label for="chk1b"><input name="chk1b" id="chk1b" type="checkbox" onchange="function1('chk1b','li1b')">Option1</label></li> <li id="li2b"><label for="chk2b"><input name="chk2b" id="chk2b" type="checkbox" onchange="function1('chk2b','li2b')">Option2</label></li> //etc. </ul> </form> k, then to put the values into an array in php: $maxcheckboxes=2; for($i=1;$i<$maxcheckboxes+1;$i++){ if(!empty($_POST['check'.$i.'b'])){ // $array[]=$_POST['check'.$i.'b']; if you don't care what number it was from one it was from. // $array['check'.$i.'b']=$_POST['check'.$i.'b']; if you want it to have the same key as the name of the checkbox. $array[$i]=$_POST['check'.$i.'b']; // you can also make an array of all the checkboxes that has been passed, so that you don't need to do that later: $keyarray[]=$i; } } print_r($array); print_r($keyarray); Link to comment https://forums.phpfreaks.com/topic/221228-checkbox-list-with-list-options-stored-in-external-array/#findComment-1145410 Share on other sites More sharing options...
RLJ Posted December 10, 2010 Author Share Posted December 10, 2010 Hi, sorry I should have been more clear. Thanks for your reply, but it seems to be the wrong way round from what I want to do! To clarify, I already have the array with the list options. I now want to modify the html list (with some PHP) so that I don't have to type each list option separately, but so that it takes its list options from the array. So I want to come up with an equivalent of the following: include 'LISTS.php'; print('<select id="from" name="from">'); foreach ($langList as $lang) {printf('<option %s>%s</option>', ($from1 == $lang ? 'selected="selected"' : ''), $lang) } echo '</select>';; for this: <form method="post" action=""> <ul style="height: 95px; overflow: auto; width: 200px; border: 1px solid #480091; list-style-type: none; margin: 0; padding: 0;"> <li id="li1b"><label for="chk1b"><input name="chk1b" id="chk1b" type="checkbox" onchange="function1('chk1b','li1b')">Option1</label></li> <li id="li2b"><label for="chk2b"><input name="chk2b" id="chk2b" type="checkbox" onchange="function1('chk2b','li2b')">Option2</label></li> //etc. </ul> </form> Cheers! Link to comment https://forums.phpfreaks.com/topic/221228-checkbox-list-with-list-options-stored-in-external-array/#findComment-1145433 Share on other sites More sharing options...
MMDE Posted December 10, 2010 Share Posted December 10, 2010 oh lol, the other way around hmm a lot of your code doesn't make too much sense for me though, I think this is very short parts of it, making it very hard to see what you actually want to do. you want it to loop making those checkboxes? what is special about each of them? are they going to be like, 1,2,3,4,5,6 or is it specific values? where do you get this array from? Link to comment https://forums.phpfreaks.com/topic/221228-checkbox-list-with-list-options-stored-in-external-array/#findComment-1145437 Share on other sites More sharing options...
RLJ Posted December 10, 2010 Author Share Posted December 10, 2010 OK, so the array will be in LISTS.php, which I will retrieve with an 'include' command. Then I want to create a checkbox list (a scrolling list of options where you can select more than one) where each option is an element from the array. So say the array is as follows: $optionList = array('Oranges', 'Bananas'); then I want the PHP script to print the following to the browser: <form method="post" action=""> <ul style="height: 95px; overflow: auto; width: 200px; border: 1px solid #480091; list-style-type: none; margin: 0; padding: 0;"> <li id="li1b"><label for="chk1b"><input name="chk1b" id="chk1b" type="checkbox" onchange="function1('chk1b','li1b')">Oranges</label></li> <li id="li2b"><label for="chk2b"><input name="chk2b" id="chk2b" type="checkbox" onchange="function1('chk2b','li2b')">Bananas</label></li> </ul> </form> and then if I add "Apples" to the array, I want the PHP script to print the same again, but with the following extra line: <li id="li3b"><label for="chk3b"><input name="chk3b" id="chk3b" type="checkbox" onchange="function1('chk3b','li3b')">Apples</label></li> so the elements are always called the same, except the number goes up by 1 for each next row in the list. Cheers. Link to comment https://forums.phpfreaks.com/topic/221228-checkbox-list-with-list-options-stored-in-external-array/#findComment-1145481 Share on other sites More sharing options...
RLJ Posted December 15, 2010 Author Share Posted December 15, 2010 Anyone? I think it should be fairly straightforward, I'm just not that familiar with for loops. Pls help! Thanks! Link to comment https://forums.phpfreaks.com/topic/221228-checkbox-list-with-list-options-stored-in-external-array/#findComment-1147833 Share on other sites More sharing options...
kenrbnsn Posted December 15, 2010 Share Posted December 15, 2010 Try this: <?php $optionList = array('Oranges', 'Bananas'); $tmp = array(); $i = 1; $tmp[] = '<form method="post" action="">'; $tmp[] = '<ul style="height: 95px; overflow: auto; width: 200px; border: 1px solid #480091; list-style-type: none; margin: 0; padding: 0;">' foreach ($optionList as $option) { $tmp[] = '<li id="li' . $i . 'b"><label for="chk' . $i . 'b"><input name="chk' . $i . 'b" id="chk' . $i . 'b" type="checkbox" onchange="function1(\'chk' . $i . 'b\',\'li' . $i . 'b\')">' . $option . '</label></li>'; $i++; } $tmp[] = '</ul>'; $tmp[] = '</form>'; echo implode("\n",$tmp) . "\n"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/221228-checkbox-list-with-list-options-stored-in-external-array/#findComment-1147849 Share on other sites More sharing options...
RLJ Posted December 19, 2010 Author Share Posted December 19, 2010 That's great, thanks! btw, semicolon missing in line 7. Link to comment https://forums.phpfreaks.com/topic/221228-checkbox-list-with-list-options-stored-in-external-array/#findComment-1149298 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.