Tim
Tim

Reputation: 63

html form option value of mysql database only stores first word

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.

Upvotes: 1

Views: 1036

Answers (1)

Rikesh
Rikesh

Reputation: 26431

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.

Upvotes: 1

Related Questions