Search code examples
javascriptphpdropbox

Use PHP in dropboxlist and it doesnt work well


I want to see data with dropboxlist . But it dosen't work well

on my dropbox, just could see "-- CHOOSE --" and "firstdata of DB"

like

(-- CHOOSE -- <- dropbox

DBlist1 ) <- dropbox

DBlist2 <-- not dropbox. just text

DBlist3 <-- not dropbox. just text

DBlist4 <-- not dropbox. just text

<?php
$var2 = $_POST['selectCarID'];
$conn = mysqli_connect("192.168.44.122", "dbtjd1", "root", "db");
$query = "SELECT Day FROM hashDB WHERE CarID = '".$var2."'";
$result = mysqli_query($conn, $query);

?>
        <select name='dayval'>
            <option value=''>-- CHOOSE --</option>
<?php
while ($row = mysqli_fetch_assoc($result)) {
    foreach ($row as $field) {

        ?>
            <option value="<?php echo "".htmlspecialchars($field).""; ?>">
                <?php echo "".htmlspecialchars($field).""; ?>
            </option>
        </select>
        <?php
    }
}
?>

I don't have idea on this ..

I really appreciate of you ! thanks.


Solution

  • Given that you're coding this on the front end use the following code to populate the drop down like so :

    <select name='dayval'>
      <option value=''>-- CHOOSE --</option>
       <?php
    
        $var2 = $_POST['selectCarID'];
        $conn = mysqli_connect("192.168.44.122","dbtjd1","root","db");
        $query = "SELECT Day FROM hashDB WHERE CarID = '".$var2."'";
        $result = mysqli_query($conn, $query);
    
         while ($row = mysqli_fetch_assoc($result)){
            $field = $row['Day'];
            echo "<option value=".htmlspecialchars($field).">".htmlspecialchars($field)."</option>"; 
          } 
        ?>
    </select>
    

    In case of an error, do post the error information and other necessary details such as: if you want to run the code on the backend instead of frontend.