简体   繁体   中英

selected option fetch data from database in php

I want to fetch data from a database related to a selected option when it is clicked.

When I select another option then fetch the data for that selection.

When I fetch the new data option the old data is replaced.

I want to display each set of data in a new row so I do not want the new data to replace the old data.

<form action="" method="post">
    <div class="form-group">
        <label for="sel1">Select list:</label>
        <select multiple name="names" class="form-control" id="sel1">
        <?php
                $result = "SELECT Name FROM coder";

                $sql = $conn->query($result);
                if ($sql->num_rows > 0) 
                   while($row = $sql->fetch_assoc()) {?>
                      <option value='<?php echo $row['Name'] ?>'><?php echo $row['Name'] ?></option> 
                 <?php  }
                    ?>                  
        </select>
        </div>   
    <input type="submit" name="submit" class="btn btn-info">
</form>


<table>
    <thead>

    </thead>
    <tbody>
    <?php
    $result = "SELECT * FROM coder WHERE Name='$name'";

        $sql = $conn->query($result);
        if ($sql->num_rows > 0) 
           while($row = $sql->fetch_assoc()) {{?>
      <tr>
            <td><?php echo $row['Name']?></td>
        </tr>

        <?php 
               }
            }
         ?>

    </tbody>
</table>

I suggest to use jquery for binding the div once the result is placed inside it.

Also Instead of using direcy Query, you also can use onChange event of jQuery for fetching the data according to your need. It will accelerate the speed of fetching of data.

Divide the div into samall child divs and store the data into them.

Your problem will be resolved in this way for sure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM