1

I was able to populate a dropdown question html form with results from my database. However, the item I am choosing is a name, and although both the first and last name show up in the menu option, when it is submitted, only the first name is showing up.

$sql = "SELECT DISTINCT name FROM communications";
$result = mysql_query($sql);
echo "<select name='client1'>";
while($row = mysql_fetch_array($result)) {
        echo "<option value=".$row['name'].">";
        echo $row['name'];
        echo "</option>";} 
echo "</select>";?>
<input type="submit" name= "submit2" value="Show">
<input type="hidden" name= "submit3" value="Submit">
</form> 
<?php
if(isset($_POST['submit3'])) 
{ echo $_POST['client1'];   }

client1 is only echoing out the first name, not the second. Can't figure out why.

Tim
  • 63
  • 1
  • 1
  • 10

1 Answers1

1

You need to quote the given string in select option value as,

while($row = mysql_fetch_array($result)) {
        $name = $row['name'];
        echo "<option value='".$name."'>";
                            ^         ^

Waring: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • Would you please consider NOT using shortened URLs in your answers? I've unshorted those same URLs a number of times now and it's getting a bit old... – Michael Kohne Jul 05 '14 at 12:57