Issue
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?
Solution
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'];
Answered By - sdo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.