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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select multiple options not working?

Status
Not open for further replies.

aweller

Programmer
Apr 25, 2006
2
US
Hello all,

I'm relatively new to .PHP and I would very much appreciate some help with a contact form issue.

I have updated this form to allow multiple selections from a drop down menu by using the <select multiple name=" "> etc, but form output is only exporting one value.

There is some previously coded .PHP involved with the form that I believe is limited the 'multiple selections' option.

Here is the link to the contact form:


and the handler:


Thank you in advance!

Adam
 
Sleipnir214, thanks for the speedy response. Unfortunately it did not solve my problem.

I added the multiple="yes" and the [] inside the name, but now the exported data says "Array" instead of the chosen selections.

Here's the code:

<select multiple="yes" name="Age_Range_Selection[]" <? print $style; ?>>
<option value="25-34" <?php formSelected( $HTTP_POST_VARS[ "Age_Range_Selection" ], "25-34" ); ?> > 25-34
<option value="35-44" <?php formSelected( $HTTP_POST_VARS[ "Age_Range_Selection" ], "35-44" ); ?> > 35-44
<option value="45-54" <?php formSelected( $HTTP_POST_VARS[ "Age_Range_Selection" ], "45-54" ); ?> > 45-54
<option value="55-64" <?php formSelected( $HTTP_POST_VARS[ "Age_Range_Selection" ], "55-64" ); ?> > 55-64
<option value="65-80" <?php formSelected( $HTTP_POST_VARS[ "Age_Range_Selection" ], "65-80" ); ?> > 65-80
</select>

The link you posted suggests:

------
This tells PHP to treat $var as an array and each assignment of a value to var[] adds an item to the array. The first item becomes $var[0], the next $var[1], etc. The count() function can be used to determine how many options were selected, and the sort() function can be used to sort the option array if necessary.

Note that if you are using JavaScript the [] on the element name might cause you problems when you try to refer to the element by name. Use it's numerical form element ID instead, or enclose the variable name in single quotes and use that as the index to the elements array, for example:
variable = documents.forms[0].elements['var[]'];
-------

However I'm not sure as to how it applies to my code.
 
Naming the form field Age_Range_Selection[] tells PHP to treat the input's values as an array.

Thus, $_POST['Age_Range_Selection'] will itself be an array. You're going to have to use an additional subscript: $_POST['Age_Range_Selection'][red][0][/red] will be the first item picked.

I strongly recommend you use $_POST instead of $HTTP_POST_VARS. The former is superglobal ( than only global, and the latter has been deprecated in newer versions of PHP.


Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top