|
-
Apr 1st, 2006, 07:30 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] PHP/HTML Pull down box
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?
Last edited by modpluz; Apr 15th, 2006 at 05:18 PM.
-
Apr 2nd, 2006, 04:26 AM
#2
Re: PHP/HTML Pull down box
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:
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>
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:
PHP Code:
if (isset($_POST['multi']) && is_array($_POST['multi'])) {
foreach($_POST['multi'] as $item) {
/* do stuff here */
}
}
-
Apr 2nd, 2006, 08:00 PM
#3
Thread Starter
Fanatic Member
Re: PHP/HTML Pull down box
thanks...help appreciated
-
Apr 15th, 2006, 05:21 PM
#4
Thread Starter
Fanatic Member
Re: PHP/HTML Pull down box
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
Code:
<script>
document.all.multi[].value;
</script>
how can i overcome this?
-
Apr 15th, 2006, 05:29 PM
#5
Re: PHP/HTML Pull down box
Give the form an ID and you can access it as follows:
Code:
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.
-
Apr 15th, 2006, 06:19 PM
#6
Thread Starter
Fanatic Member
Re: PHP/HTML Pull down box
thanks but i found another error when i do this
Code:
theForm.elements[''multi[]'].selectedIndex;
the error say it is null or not an object.
-
Apr 15th, 2006, 07:02 PM
#7
Re: PHP/HTML Pull down box
Did you give the form an ID?
ie:
Code:
<form id="theForm">
-
Apr 15th, 2006, 07:27 PM
#8
Thread Starter
Fanatic Member
Re: PHP/HTML Pull down box
-
Apr 15th, 2006, 07:36 PM
#9
Thread Starter
Fanatic Member
Re: PHP/HTML Pull down box
got it... the problem is that the javascript in in an external .js, which means saying something like
Code:
document.frmForm.elements['multi[]'];
will pop up an error(i guess)
but thanks its now working fine
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|