PDA

Click to See Complete Forum and Search --> : [RESOLVED] PHP/HTML Pull down box


modpluz
Apr 1st, 2006, 06:30 PM
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?

visualAd
Apr 2nd, 2006, 04:26 AM
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:

<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>

The hidden field is optional. It just ensures the presence of a multi varaible in the $_POST array, even if nothing was selected.

To find out what was selected, if any, do the following:

if (isset($_POST['multi']) && is_array($_POST['multi'])) {
foreach($_POST['multi'] as $item) {
/* do stuff here */
}
}

modpluz
Apr 2nd, 2006, 08:00 PM
thanks...help appreciated

modpluz
Apr 15th, 2006, 05:21 PM
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

<script>
document.all.multi[].value;
</script>

how can i overcome this?

visualAd
Apr 15th, 2006, 05:29 PM
Give the form an ID and you can access it as follows:

var theForm = document.getElementById('theForm');

Var value = theForm.elements['multi[]'].value;

Suare brackets are Jvascript meta characters, you therefore need to access the element via the elements array of the form object.

modpluz
Apr 15th, 2006, 06:19 PM
thanks but i found another error when i do this

theForm.elements[''multi[]'].selectedIndex;

the error say it is null or not an object.

visualAd
Apr 15th, 2006, 07:02 PM
Did you give the form an ID?

ie:

<form id="theForm">

modpluz
Apr 15th, 2006, 07:27 PM
yes i did

modpluz
Apr 15th, 2006, 07:36 PM
got it... the problem is that the javascript in in an external .js, which means saying something like

document.frmForm.elements['multi[]'];

will pop up an error(i guess)
but thanks its now working fine