Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Post Data from Multiple Select on Form

Status
Not open for further replies.

cmhunt

Programmer
Joined
Apr 17, 2001
Messages
119
Location
GB
Hi

I have a form something like:
Code:
<form action=&quot;submit.php&quot; method=&quot;post&quot;>
  <select name=&quot;choice&quot; size=&quot;10&quot; multiple>
    <option>1
    <option>2
    <option>3
  </select>
  <input type=submit value=&quot;Go!&quot;>
</form>
and in the submit page, I collect the values from &quot;choice&quot;. However the Select is multiple and I can't figure out how to pick up all the selected values. It only seems to pick up the last value with $_POST[&quot;choice&quot;].

Any help is appreciated.

Thanks

Chris
 
You need to name the INPUT element accordingly with square brackets after the name to make it an array:
Code:
<select name=&quot;choice
Code:
[]
Code:
&quot; size=&quot;10&quot; multiple>

That's all.
 
u have to use javascript.

have a hidden field.

<head>
<script>
function SelChoice()
{
vl=&quot;&quot;
len=document.frm.choice.length
for(i=0;i<len;i++)
{
if(document.frm.choice.selected==true)
{
vl+=document.frm.choice.value+&quot;///&quot;;
}
}
document.frm.hchoice.value=vl
}
</script>
</head>
<form name=&quot;frm&quot; action=&quot;submit.php&quot; method=&quot;post&quot; onsubmit=&quot;SelChoice()&quot;>
<select name=&quot;choice&quot; size=&quot;10&quot; multiple>
<option>1
<option>2
<option>3
</select>
<input type=submit value=&quot;Go!&quot;>
<input type=&quot;hidden&quot; name=&quot;hchoice&quot;>
</form>


in the next page:
<?
$choice=split(&quot;///&quot;,$_POST['hchoice']);
for($i=0;$i<count($choice);$i++)
{
if($choice[$i]!=&quot;&quot;)
echo $choice[$i].&quot;<BR>&quot;;
}
?>

Known is handfull, Unknown is worldfull
 
vbkris

I disagree that you have to use JavScript. In fact the array indicator in the SELECT tag will in all cases translate to an array when posted to PHP.
With the JavaScript method you have more steps to do, namely:
1. convert upon submission to a string literal. You depend on the browser being JavaScript enabled.
2. PHP: convert the posted string literal into an array.

I believe that the [] method is more efficient and always available.

 
Cheers guys. Two little square brackets did the job!! Worrying when it's the HTML that's letting you down!! :-)

Chris
 
DRJ478:
only one problem with [] when using it in javascript for some kind of validation (NOTE: DOM Not allowed) then it thros up an erro if i try something like this:
alert(document.FormName.SelectBox[].value)

how do i overcome this?

Known is handfull, Unknown is worldfull
 
vbkris:

I believe there is a workaround by also setting an id parameter in the <SELECT> tag. When you give it an ID you can access the element in DOM without any trouble.
 
If you have an input tag with both the name and id attributes set, you can reference the element in JavaScript using the id attribute just as if it were the name of the element.

alert (document.formname.elementid.value);

will work. At least, it does for me using both IE and Opera.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
sometimes i feel like [hammer] myself....

Known is handfull, Unknown is worldfull
 
and in php what must i use to get the values?
$_POST[name] or $_POST[id]?

if only id is given can i get the value in php?

Known is handfull, Unknown is worldfull
 
thanks...
are u ruling out $_POST['id']; (i am asking this as u have made no comment on it)... :)


Known is handfull, Unknown is worldfull
 
You question asked which you must use. I have answered with a complete list.


Buy why are you asking me, when you could find out for yourself in less than 2 minutes? Create a form with both element names and element ids. Submit the form to a PHP script, and in that script perform a print_r() on either $_POST or $_GET as appropriate.


Want the best answers? Ask the best questions: TANSTAAFL!!
 
[rainbow]...

sorry sleipnir. the system from which i was typing did not have PHP. so couldnt check it up. i wanted an immedeate answer. sorry if i have offened u... :(

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top