I want to get records from database to fill form for update. I dont have problem with the input fields but for the select option. I want to click on an icon to fetch the data to fill the select option with the selected="selected" base on a specific ID from the db. Please help.
Thanks
<select name="cat_id">
<option></option>
<?php $query = mysql_query("SELECT * FROM account_type");
while($row = mysql_fetch_array($query)) { ?>
<option value="<?php echo$row['cat_id']?>" selected="selected" >
<?php echo$row['category']?></option>;
<?php } ?>
</select>
Like this?
<select name="cat_id">
<option></option>
<?php
$cat_id = 4; // The selected cat
$query = mysql_query("SELECT * FROM account_type");
while($row = mysql_fetch_array($query)) {
if($row['cat_id'] == $cat_id) {
?>
<option value="<?php echo $row['cat_id']?>" selected="selected"><?php echo $row['category']; ?></option>
<?php
} else {
?>
<option value="<?php echo $row['cat_id']?>"><?php echo $row['category']; ?></option>
<?php
}
}
?>
</select>
<select name="cat_id">
<option></option>
<?php
$cat_id = 1; // The selected cat
$query = mysql_query("SELECT * FROM account_type");
while($row = mysql_fetch_array($query)) {
?>
<option value="<?php echo $row['cat_id']; ?>" <?php if($row['cat_id'] == $cat_id) { echo "selected"; ?>><?php echo $row['category']; ?></option>
<?php
}
?>