0

I am trying to provide drop down list in input field using values from MySQL. I tried a lots of examples from web but no one works. Here is my code.

<div class="form-group">
            <input autocomplete="off" autofocus class="form-control" name="date" id="datepicker" placeholder="Date" type="text"/>
            <input autocomplete="off" autofocus class="form-control" name="diagnosis" placeholder="Diagnosis" type="text"/>
            <input autocomplete="off" autofocus class="form-control" name="therapy" placeholder="Therapy" type="text"/>
            <input autocomplete="off" autofocus class="form-control" name="comment" placeholder="Comment" type="text"/>
            <input list="doctors" autofocus class="form-control" name="doctor" placeholder="Doctor" type="text"/>
              <datalist id="doctors">
                <?php $result= CS50::query("SELECT name FROM doctors");
                foreach($result as $result): ?>
                 <option value="$result["name"]">
                <?php endforeach ?>
              </datalist>
        </div>

or

 $names= CS50::query("SELECT name FROM doctors");
            while ($row=mysql_fetch_array($names)) {
            $name=$row["name"];
                echo "<option>
                    $name
                </option>";
            }

Thanks for help!

1 Answer 1

1

This is sketchy: foreach($result as $result): ?> because the var names are the same (and I'm not sure about : in php). Wouldn't the proper syntax here <option value="$result["name"]"> be <option value="<?= $result['name'] ?>">? Syntax of the echo is not what you want in the second version since it will literally echo "<option> $name </option>". Should be something like "<option>".$name."</option>"

1
  • Thanks! Yes, the problem was in <option value="$result["name"]"> be <option value="<?= $result['name'] ?>">
    – Ivanka D.
    Aug 2, 2017 at 7:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .