Steph
Steph

Reputation: 25

How do I get a drop down/select option have Multiple Values

I've read other questions on here that dealt with what I am looking for. But it still did not help me. I want to have multiple values in an option of the select tag of a form that will be posting the information onto a different file that is viewable to the visitor by iframe. Example of what I am trying to do:

The following is on the file: Form.php

<div>
<form action"blank.php" method="post" target="box">
<select name="g1">
<option name="g1" value="Value 1, Value 2"> Text </option>
<option name="g1" value="Value 1, Value 2"> Text Blah </option>
</select>
<input type="submit" />
</form>
</div>
<iframe src="blank.php" name="box"></iframe>

The Following is a long the lines of what I want to appear on the: blank.php which is visible by iframe.

Option 1 or 2 value's should appear like this

[ Value 1 ] [ Value 2 ]

An Example:

Its a drop down menu that will post two seperate values when you select one option. Like if I selected an option that says Hola - hello and it will post:

[Hola] means [Hello].

and the option tag would have the values as:

<option name="g1" value="Hola, Hello">Hola - Hello</option>

Upvotes: 1

Views: 15125

Answers (3)

j08691
j08691

Reputation: 207901

Sounds like you want to build an array of your form fields where multiple values are allowed. Read the PHP manual for more.

From the manual:

How do I get all the results from a select multiple HTML tag?

The select multiple tag in an HTML construct allows users to select multiple items from a list. These items are then passed to the action handler for the form. The problem is that they are all passed with the same widget name. I.e.

<select name="var" multiple="yes">

Each selected option will arrive at the action handler as:

var=option1
var=option2
var=option3

Each option will overwrite the contents of the previous $var variable. The solution is to use PHP's "array from form element" feature. The following should be used:

<select name="var[]" multiple="yes">

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 = document.forms[0].elements['var[]'];

Upvotes: 2

James L.
James L.

Reputation: 4097

I know that this is in the PHP section, but what you are trying to do would be much easier to achieve with javascript. If you change the iframe tags to:

<div id="valueDisplay"></div>

and add an ID and listener to the select

<select name="g1" id="dropDown" onchange="showValues()">
<option name="g1" value="1"> Text </option>
<option name="g1" value="2"> Text Blah </option>
</select>

You just write a little script like this and put it in the body somewhere:

<script type="text/javascript">
function showValues(){
    var outDiv = document.getElementById('valueDisplay');
    var selectValue = document.getElementById('dropDown').value;
    var divText;
    if(selectValue == 1) divText = '[ Value 1 ][ Value 2 ]';
    if(selectValue == 2) divText = '[ Value 1 ][ Value 2 ]';
    outDiv.innerHTML = divText;
}
</script>

To achieve what you are trying to do with iframes would be more difficult. It would require you to use javascript/jquery to post the data within the iframe.

Note that using onchange="" is no longer the accepted method of adding listeners. It is just easier if you are not familiar with javascript.

Upvotes: 0

sdleihssirhc
sdleihssirhc

Reputation: 42496

First of all, you don't need a name attribute on each of your <option> elements. Just the <select> box.

Second of all, I think all you need is the handy dandy explode function. You'll get the value like this...

$values = $_POST['g1'];

...and then split them into an array like this...

$values = explode(', ', $values);

...and finally just access the values like so:

$value1 = $values[0];
$value2 = $values[1];

Upvotes: 1

Related Questions