Khurram Hafeez
Khurram Hafeez

Reputation: 21

Creating select list from database

I am making select list from database table everything works fine only issue is in option value if the value consist of two words like "New York" it only returns "New". Here is the code

<?  $country=intval($_GET['country']);

include('connect-db.php');

$query="SELECT CityName FROM cities WHERE CountryId='$country'";
$result=mysql_query($query);

?>
<select name="city">
<option value="">Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['CityName']?>><?=$row['CityName']?></option>
<? } ?>
</select>

Upvotes: 0

Views: 5476

Answers (4)

Salman
Salman

Reputation: 1380

You haven't included the "" while giving the values. if you look at the html created for the select box by your code is something like

<option value="new" york>new york</option>

correct solution is given below, just included the quotation while giving value of option tag.

<?  $country=intval($_GET['country']);

include('connect-db.php');

$query="SELECT CityName FROM cities WHERE CountryId='$country'";
$result=mysql_query($query);

?>
<select name="city">
<option value="">Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>
<? } ?>
</select>

Upvotes: 1

HTML Man
HTML Man

Reputation: 937

Just use "" for the value of value same as you write HTML tag.

Upvotes: 0

rvil
rvil

Reputation: 136

You'll have to surrond the value in "", the HTML code thinks the York part is just another attribute of the option tag, so:

<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>

Upvotes: 1

sel
sel

Reputation: 4957

<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>

The " " .

Upvotes: 1

Related Questions