I have this select in my form:
<select id="select" name="gametype" onchange="myFunction(event)">
<option disabled selected value>Please Select Game Type</option>
<option value="28,000" >HD 6 Cameras saterday</option>
<option value="30,000" >HD 6 Cameras</option>
<option value="50,0000">HD 7 Cameras saterday</option>
<option value="32,0000">HD 7 Cameras</option>
<option value="45,000" >4k 7 Cameras saterday</option>
<option value="32,000" >4k 7 Cameras</option>
</select>
I am passing the value of the option in $_POST
. How can I also pass the text of the option? For example, if the value is "28,800" I also want to pass "HD 6 Cameras saterday."
Not sure how you're handling the other components of this form, but I removed the onchange attribute binding from the select element and just used jquery to set the text displayed to the user as a hidden variable.
<?php
if(isset($_POST['gametype'])) {
var_dump($_POST);
echo "<hr/>";
}
?>
<script
src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
crossorigin="anonymous"></script>
<form class="myForm" method="post">
<!--this is empty and stays that way until you submit it-->
<input type="hidden" name="gametype_desc" class="gametype_desc" value="" />
<select id="select" class="selGameType" name="gametype">
<option disabled selected value>Please Select Game Type</option>
<option value="28,000" >HD 6 Cameras saterday</option>
<option value="30,000" >HD 6 Cameras</option>
<option value="50,0000">HD 7 Cameras saterday</option>
<option value="32,0000">HD 7 Cameras</option>
<option value="45,000" >4k 7 Cameras saterday</option>
<option value="32,000" >4k 7 Cameras</option>
</select>
</form>
<script>
$(function(){
$("select.selGameType").change(function(){
//uncomment to log the value in the console
//console.log($(this).children(":selected").text());
//uncomment to log the value in the console
//console.log($(this).val());
//get the text of the currently selected option
selText = $(this).children(":selected").text();
//we set the value of the hidden variable, so that the text (not value) is passed as a separate post var
$("input.gametype_desc").val(selText);
//now you might submit it, or whatever your next step is...
$("form.myForm").submit();
})
})
</script>