0

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>

1 Answer 1

1

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>";      
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.