lazersquids mcgee
lazersquids mcgee

Reputation: 313

how can i get a variable value into a select tag in html using php

so the line of code

    echo "<option value = '$name'>$name</option>";

is where i am having a problem. I am trying to get drop down menu to have the values of every name in the database. here is my code, it is in a html file:

<select name = "author" id = "author">
                <?php 
                    $servername = "localhost";
                    $username = "user";
                    $password = "pass";
                    $database = "db";

                    $con = mysqli_connect($servername,$username,$password,$database);
                        if($con->connect_error){
                        die("Connection failed " . $con->connect_error);
                    } 
                    $sql = "select first, last from Employee";
                    $result = mysqli_query($con,$sql);
                    while ($row = mysqli_fetch_array($result)) {
                        $name = $row['first'] . ' ' . $row['last'];
                        echo "<option value = '$name'>$name</option>";

                        /*echo <<<EOT 
<option value = '$name'>$name</option>"
EOT;*/
                    }
                ?>
            </select>

I have tried it using a normal string, and as an EOT. Currently when i look at the html page it shows "$name" instead of the actual name. I have done output testing and $name is storing what i expect it to store.

Upvotes: 0

Views: 403

Answers (3)

PHPhil
PHPhil

Reputation: 1540

I just take a step in the dark, You saved your file as HTML (.html) and not as a PHP file (.php)?

Upvotes: 3

GreenGenie
GreenGenie

Reputation: 56

I remember there being something about PHP not processing variables between single quotes. The best way may be to break and concat the string

echo "<option value = '".$name."'>$name</option>";

Upvotes: 0

Anton
Anton

Reputation: 1061

Try:

echo "<option value = '".$name."'>".$name."</option>";

or

echo "<option value = '{$name}'>{$name}</option>";

Upvotes: 0

Related Questions