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?
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?
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 php
You are not signed in. Login or sign up in order to post.
Where is the
select
in your code?– Wallace Maxters
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>
– heitor_deep
You want to make a SELECT with multiple options, or you want to take the value that is in
value
and between the tagoption
? I don’t think I got that part right.– Wallace Maxters
That, I want to take the two results, first of the value and then what is within the option
– heitor_deep
But do you want to do this for PHP or Javascript? And the values are different?
– Wallace Maxters
PHP, yes in value I need to put numbers and option text
– heitor_deep
Let’s go continue this discussion in chat.
– Wallace Maxters
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.– Woss
Since the author of the question has made it clear that the problem is another, I am voting to close the question.
– Wallace Maxters