Take data from option

Asked

Viewed 35 times

1

People ask me a question. How do I get 2 values of an option in php?

example :

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

I have tried using foreach but not getting it only comes the value. Someone has some idea?

  • Where is the select in your code?

  • sorry, I just put the main part of the code in this way. <select name="service[]"> <option value="pick up here">(Pick up here)</option> </select>

  • You want to make a SELECT with multiple options, or you want to take the value that is in value and between the tag option? I don’t think I got that part right.

  • That, I want to take the two results, first of the value and then what is within the option

  • But do you want to do this for PHP or Javascript? And the values are different?

  • PHP, yes in value I need to put numbers and option text

  • 1

    Only the value in value is sent in the request. The content within the tags serves only to better inform your user. If both values do not represent the same thing, your system is inconsistent.

  • 1

    Since the author of the question has made it clear that the problem is another, I am voting to close the question.

Show 4 more comments

1 answer

0

The form will submit only the values.

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

A simple idea:

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

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

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

In PHP it normally receives and abstracts the 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 using Javascript, at the time you submit the form, before actually being sent, Javascript would read the elements and create Hidden fields linked in some way with that element.

Then in the PHP that receives the data would verify these links by checking 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, concaten the label to the value at the moment the page is loaded. So the original value doesn’t necessarily have to come with the label included in value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.