Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, I have this code which can fetch data from database:
C#
<?php
            //connect to database
            $conn = mysqli_connect("localhost", "root", "", "inventory");
          
            if(mysqli_connect_errno($conn)) { 
              echo "Unable to connect to database server";
            }    
            
            //query database for items to populate
            $sql = "SELECT ITEM_NAME FROM item";
            $query = mysqli_query($conn, $sql);
            
            echo '<datalist name="list1" id="list1" onchange="showUser(this.value)">'; //change select to datalist
            echo '<option value="">Select item</option>';
            
            while($selectedItem = mysqli_fetch_assoc($query)) {
              echo "<option>{$selectedItem['ITEM_NAME']}</option>";
            }

            echo '</datalist>'; //change select to datalist

            if (isset ($_POST['submit'])) {
              $selectedItem = $_POST['list1'];
            }

    ?>


In this part echo "<option>{$selectedItem['ITEM_NAME']}</option>";
I'm displaying the data from the database, but I need to get the corresponding value from the datalist.

Hope to hear a lot from you guys! Thanks!

What I have tried:

I tried to do it like this
echo "<option value=".$selectedItem.">{$selectedItem['ITEM_NAME']}</option>";
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900