Get Text From <option> Tag Using PHP

Abdul wahab picture Abdul wahab · Jul 20, 2011 · Viewed 36.4k times · Source

I want to get text inside the <option> tags as well as its value.

Example

<select name="make">
<option value="5"> Text </option>
</select>

I used $_POST['make']; and I get the value 5 but I want to get both value and the text.

How can I do it using PHP?

Answer

serialworm picture serialworm · Jul 20, 2011

In order to get both the label and the value using just PHP, you need to have both arguments as part of the value.

For example:

<select name="make">
    <option value="Text:5"> Text </option>
</select>

PHP Code

<?php
$parts = $_POST['make'];
$arr = explode(':', $parts);

print_r($arr);

Output:

Array(
  [0] => 'Text',
  [1] => 5
)

This is one way to do it.