whitespace in select dropdown, pulled from mysql
I'm using this to populate a dropdown:
$result = mysql_query("SELECT user from `users` order by user asc") or die(mysql_error());
echo '<select name="user" class="user">';
while ($row = mys开发者_StackOverflowql_fetch_array($result)) {
echo "<option value=".$row['user'].">".$row['user']."</option>";
}
echo '</select>';
But the source is this:
<select name="user" class="user"><option value=joe bloggs>joe bloggs</option>
So when i do this:
var user = $('.user').val();
It only sees "joe" not "joe bloggs"?? Any ideas
$result = mysql_query("SELECT user from `users` order by user asc") or die(mysql_error());
echo '<select name="user" class="user">';
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.htmlspecialchars($row['user']).'">'.htmlspecialchars($row['user']).'</option>';
}
echo '</select>';
Corrected for you :)
Wrap the value in quotes:
echo "<option value=\"".$row['user']."\">".$row['user']."</option>";
Also, make sure to htmlspecialchars()
in order to escape possible quotes in the name. E.g.,
$user = htmlspecialchars($row['user']);
printf('<option value="%s">%s</option>', $user, $user);
inside the while loop try:
echo "<option value=\"".$row['user']."\">".$row['user']."</option>";
basically adding a quote to the value inside the option
Use quotes "
around attribute values..
your html should look like
<option value="joe bloggs">joe bloggs</option>
精彩评论