0

This below is the code I am using to display a dropdown dynamically.But I am unable to retain the value of the dropdown after posting it?I am able to insert the values and able to retain the values for textboxes..but unable to retain for dropdown

Available Doctors

            <?php 
            $selectedDoctor = $_POST["doctortype"];
            $sql = "select firstname from users where userid in (select userid from doctortype where doctortype = \"$selectedDoctor\");";
            include_once("inc_databaseClass.php");
            $dClass=new databaseClass;
            $results=$dClass->connect();
            $result = mysqli_query($results,$sql);
            echo "<select name = 'selectDoctors' id = 'idaname'>"; 
            echo "<option value = ''>";

            while ($row = mysqli_fetch_array($result)) {

            $selected = (isset($_POST['selectDoctors']) && $_POST['selectDoctors'] == $row['firstname']) ? 'selected = "selected"' :'';
            ?>
            <option <?php echo $selected; ?> value = "<?php echo $row['firstname']; ?>"> <?php echo $row['firstname']; ?> </option>
            <?php } ?>
            </select>

        </p>
kate
  • 11
  • 2
  • Change double quotes inside your query to single quotes, or do the professional thing and implement prepared statements with placeholders for security reasons. Start with some basic debugging write some error checking and echo the variables before you try to use them. I would write `(isset($_POST['selectDoctors'])` before the loop and if false set a variable to an empty string. – mickmackusa Apr 18 '18 at 20:54
  • I also wouldn't bounce in and out of php in your loop it makes the code harder to read. – mickmackusa Apr 18 '18 at 21:01

0 Answers0