Reputation: 651
Below is my code. It is Editor for user profile page as admin. I want to show to as selected. I read answers here. I tried, but it's not working for me. Whenever it comes close, it shows me BLANK in dropdown list.
<select name="city">
<?php
foreach( $province_array as $name)
{
if($name == $city )
{
echo '<option '.'value="'.$name.'"'.'selected="selected"'.'>'.$name.'</option>';
}
else
{
echo '<option '.'value="'.$name.'"'.'>'.$name.'</option>';
}
}
?>
and here
$city = $row['city'];
Suggestions plz!!
Upvotes: 1
Views: 569
Reputation: 716
Appending a string that way always features a high level of risk that you may forget a space somewhere so I personally think that putting a string that needs single quotes inside double quotes like this:
echo "<option selected='selected'>".$name."</option>";
This makes it look so much more like normal HTML
Upvotes: 0
Reputation: 296
echo '<option'.'selected="selected"'.'>'.$name.'</option>';
here option and selected is not seperated. You can insert a blank btw them :
echo '<option'.' selected="selected"'.'>'.$name.'</option>';
make it for others too
Upvotes: 0
Reputation: 219884
Your echo statements are poorly constructed. There's no spaces between tag names and attributes. Try:
if($name == $city )
{
echo '<option selected="selected">'.$name.'</option>';
}
else
{
echo '<option value="'.$name.'">'.$name.'</option>';
}
Upvotes: 0
Reputation: 4913
I think what you need to do is put a space between the opening of your <option>
tag and the selected
attribute. Check our your HTML source in the browser after you page has rendered.
<select name="city">
<?php
foreach( $province_array as $name)
{
if($name == $city )
{
echo '<option'.' selected="selected"'.'>'.$name.'</option>';
}
else
{
echo '<option'.' value="'.$name.'".'>'.$name.'</option>';
}
}
?>
Upvotes: 0
Reputation: 13725
You should add a few more spaces here. Change this:
echo '<option'.'selected="selected"'.'>'.$name.'</option>';
to this for example:
echo '<option '.'selected="selected"'.'>'.$name.'</option>';
Upvotes: 1