pls, can somebody show me how to read in the values of a multi-select pull down menu.
if a user should select multiple values from a pull down box, how do i read it in?
Printable View
pls, can somebody show me how to read in the values of a multi-select pull down menu.
if a user should select multiple values from a pull down box, how do i read it in?
You need to append [] to the name of the list box. PHP will then load all the select values into an array on the server:
The hidden field is optional. It just ensures the presence of a multi varaible in the $_POST array, even if nothing was selected.HTML Code:<input type="hidden" name="multi" value="" />
<select name="mutli[]" multiple="multiple" size="10">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
<option value="5">Option 5</option>
<option value="6">Option 6</option>
<option value="7">Option 7</option>
<option value="8">Option 8</option>
<option value="9">Option 9</option>
<option value="10">Option 10</option>
</select>
To find out what was selected, if any, do the following:
PHP Code:if (isset($_POST['multi']) && is_array($_POST['multi'])) {
foreach($_POST['multi'] as $item) {
/* do stuff here */
}
}
thanks...help appreciated
i got that working fine...but i now have a problem in using the checkbox/listbox with javascript
e.x.this results in an error
how can i overcome this?Code:<script>
document.all.multi[].value;
</script>
Give the form an ID and you can access it as follows:
Suare brackets are Jvascript meta characters, you therefore need to access the element via the elements array of the form object.Code:var theForm = document.getElementById('theForm');
Var value = theForm.elements['multi[]'].value;
thanks but i found another error when i do this
the error say it is null or not an object.Code:theForm.elements[''multi[]'].selectedIndex;
Did you give the form an ID?
ie:
Code:<form id="theForm">
yes i did
got it... the problem is that the javascript in in an external .js, which means saying something like
will pop up an error(i guess)Code:document.frmForm.elements['multi[]'];
but thanks its now working fine