Reputation: 2785
I am trying to show dropdown values given in MySQL via PHP dynamically. The options are coming up properly but when selecting a value and submitting the form, the value which has two words with space is saving only the first word but not the whole word.
For Eg:
Select Cars:
The dropdown has the following taken from MySQL:
Volvo XC90
Saab 95
Mercedes SLK
Audi TT
When I select Volvo XC90 and submit the form, data gets saved to DB with a value just Volvo I don't XC90.
Not sure why it is not taking white space.
PHP code that pulls data dynamically from DB:
<select name="user_zone" class="form-control ">
<?php
$zone_query = "SELECT * FROM zone_details ORDER BY zone ASC;";
$zoneresult = mysqli_query($bd,$zone_query);
while($r = mysqli_fetch_assoc($zoneresult))
{
if ($user_zone == $r['zone'])
{
$selected = 'selected="selected"';
}
else {
$selected = '';
}
echo "<option ".$selected." value=".$r['zone'].">".$r['zone']."</option>"; }
?>
</select>
Upvotes: 0
Views: 35
Reputation: 147146
Your problem is that you are not enclosing your option value
attribute in quotes, so it is taking the first word as the value and assuming the other part is another attribute. Try this instead:
echo "<option $selected value=\"{$r['zone']}\">{$r['zone']}</option>";
Upvotes: 1