Steph Steph - 5 years ago 204
PHP Question

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

<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>
<input type="submit" />
<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>

Answer Source

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];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download