M.chaudhry
M.chaudhry

Reputation: 651

Dropdownlist value is not getting selected.

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

Answers (5)

yahyazini
yahyazini

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

Akın Yılmaz
Akın Yılmaz

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

John Conde
John Conde

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

Crackertastic
Crackertastic

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

Lajos Veres
Lajos Veres

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

Related Questions