RKh
RKh

Reputation: 14161

How to retrieve DropDown value when it is posted?

When the Form is submitted, I can retrieve the DropDown text using $_POST['....']. But I want to get the DropDown value not the display member. How to get it using POST?

Upvotes: 0

Views: 11586

Answers (3)

BalusC
BalusC

Reputation: 1108742

You need to specify it in the value attribute of the <option> element. E.g.

<select name="dropdownname">
    <option value="valueYouWant1">Label which is displayed 1</option>
    <option value="valueYouWant2">Label which is displayed 2</option>
    <option value="valueYouWant3">Label which is displayed 3</option>
</select>

This way the selected value (one of the valueYouWant* values) is available by $_POST['dropdownname'].

Upvotes: 1

Gumbo
Gumbo

Reputation: 655269

If you’re talking about the SELECT element, the value of an option is the value of the value attribute or the content of that OPTION element itself if value is missing:

<option value="foo">bar</option> <!-- value is "foo" -->
<option>baz</option>             <!-- value is "baz" -->

So declare a value attribute for your options if you don’t want to contents to be the values of your options.

Upvotes: 1

Wim
Wim

Reputation: 11252

You probably need to set a value attribute to your options, with HTML like this:

<select name="foo">
  <option value="bar">Some text</option>
</select>

your $_POST array will contain 'foo' => 'bar'.

Upvotes: 2

Related Questions