Get data from the option [closed]

1

Galera makes me doubt it. How do I get 2 values from an option in php?

example:

<option value="pegar aqui">(Pegar aqui)</option>

I already tried to use foreach but not to get only the value comes. Anyone have any ideas?

    
asked by anonymous 07.04.2017 / 03:05

1 answer

0

The form will only submit values.

To get the labels of the "option" type elements, you have to create some logic involving avaScript or a gambiarra in the element's own "value" attribute.

A simple idea:

<option value="valor">label</option>

You want to get "value" and "label" then you could do something like this

<option value="valor:label">label</option>

In PHP, it normally receives and abstracts data with string manipulation functions

$str = $_POST['nome_do_select'];
$str_arr = explode(':', $str);
echo $str_arr[0]; // valor
echo $str_arr[1]; // label

An alternative to this is to use JavaScript, at the time of submitting the form, before actually submitting it, JavaScript would read the elements and create hidden fields in some way linked to that element.

Then in PHP receiving the data, I would check these links for the existence of these hidden fields. I find this more complicated. I recommend solving it in the simplest way.

If you want you can still merge both logics.

Using JavaScript, concatenate the label to the value at the time the page loads. So the original value does not necessarily have to come with the label included in the value.

    
07.04.2017 / 10:31