Jump to content

checkbox list with list options stored in external array


RLJ

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.