0

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

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Kishan Lal
  • 473
  • 1
  • 5
  • 13
  • 3
  • The best you can probably do is AJAX request to the backend and store the value in the session. Although you're probably better off just using AJAX to send the variable, *not* storing it and returning the result that it would be used for. – VLAZ Mar 24 '20 at 09:28

0 Answers0