I want to get the value of the select option and store that value in a php variable. But I don't want to use a submit button and get the value using POST or GET method. I want to get the select value dynamically and store it in a php variable which will be used in a SQL query later on.
Here is my code:
admin-panel.php
<div class="col-md-4"><label for="doctor">Doctors:</label></div>
<div class="col-md-8">
<select name="doctor" class="form-control" id="doctor" required="required">
<option value="" disabled selected>Select Doctor</option>
<?php
global $con;
$query = "select * from doctb";
$result = mysqli_query($con,$query);
while( $row = mysqli_fetch_array($result) ) {
$username = $row['username'];
$price = $row['docFees'];
$spec = $row['spec'];
echo '<option value="' .$username. '" data-value="'.$price.'" data-spec="'.$spec.'" onclick="showdocname()">'.$username.'</option>';
}
?>
</select>
</div><br/><br/>
<script type="text/javascript">
function showdocname(){
var doctor_name = document.getElementById('doctor').value;
// I want to store the value of 'doctor_name' variable in a php variable
}
</script>
Any suggestions are welcome