Nivra Zedlav
Nivra Zedlav

Reputation: 21

how to fetch the other values in the database equivalent to the selected dropdown item

This is my working code that fetches the data of the id_no in the database. Now, what I want is when I selected a specific id_no the name of the person owning that id_no will automatically be printed. :

<?php
     $sql = "SELECT id_no from employee ORDER BY id_no asc";
     $result = $conn->query($sql);
?>

        <select name='id_no'>
           <?php while ($row = $result->fetch_array()) { ?>
           <option value="id_no" > <?php echo($row['id_no']); ?> </option> 
           <?php }  ?> 
        </select>

Thanks so much!

Upvotes: 0

Views: 92

Answers (1)

ryvasquez
ryvasquez

Reputation: 158

this code will display the selected value. try this

<?php
    $sql = "SELECT id_no from employee ORDER BY id_no asc";
    $result = $conn->query($sql);
?>

    <select name='id_no' onchange="changeSelect(this.value)">
       <?php while ($row = $result->fetch_array()) { ?>
       <option value="<?php echo($row['id_no']); ?>" > 
           <?php echo($row['id_no']); ?> 
       </option> 
       <?php }  ?> 
    </select>

    <div id="demo"></div>
    <input type="text" id="text-input" />


    <script>
    function changeSelect(value)
    {
       document.getElementById("demo").innerHTML = value;
       document.getElementById("text-input").value = value;
    }
    </script>

Upvotes: 1

Related Questions