Reputation: 23
I have simple html:
<select id="rodzajp" name="Rodzaj_pakietu"">
<option value="10">Pak 1</option>
<option value="20">Pak 2</option>
<option value="30">Pak 3</option>
<option value="40">Pak 4 bla </option>
<option value="50">Pak 5 bla bla</option>
</select>
and php:
$Rodzajp = $_POST['Rodzaj_pakietu'];
With these lines I get value of chosen option. How to get with php chosen option text (Pak 1, Pak 2, Pak 3...) instead of value (10, 20, 30..), which I need for math in form?
Upvotes: 2
Views: 2488
Reputation: 662
I think since you don't get to have a value on the option field, you might want to try some javascript. For example, try making a hidden field in your form, and then have JS on your option tags like:
onclick="document.getElementById('my_hidden_field').value = 10";
That probably has issues with it.. you might have to play around but I hope you see what I am saying - use JS to dynamically change the hidden input value to what you want, and then grab THAT value from the other side with PHP like:
$Rodzajp = $_POST['my_hidden_field'];
EDIT:
After thinking about it some more, the onclick event isn't going to work on the option field.. this code should do what we want though:
JS:
function changeFunc() {
var selectBox = document.getElementById("rodzajp");
var selectedValue = selectBox.options[selectBox.selectedIndex].value;
document.getElementById('my_hidden_field').value = selectedValue;
}
HTML:
<select id="rodzajp" name="Rodzaj_pakietu" onchange="changeFunc();">
<option value="10">Pak 1</option>
<option value="20">Pak 2</option>
<option value="30">Pak 3</option>
<option value="40">Pak 4 bla </option>
<option value="50">Pak 5 bla bla</option>
</select>
<input type="hidden" value="" id="my_hidden_field" />
PHP:
$Rodzajp = $_POST['my_hidden_field'];
Upvotes: 2
Reputation: 360872
Unless PHP generated that <select>
in the first place, there's only one way to have the text submit with the form:
<select>
<option>Sometext</option>
</select>
Since there's no value
attribute on the option tag, browsers will send Sometext
instead.
Once a value
parameter is set, only that value is sent. You could hack in some javascript to copy both bits of data into the form as it submits, but that's an ugly hack.
Upvotes: 3
Reputation: 3776
The browser returns only what is in the value parameter for the OPTION tag. If you want to know what "10" was, you need to have an array on your server with the conversion.
Please note, you also have a extra " in your "name" attribute.
Upvotes: -1