Reputation: 560
please help me. I include here (see bellow) the Combobox script for INSERTING FORM I have a Form of Adding a new product. One of option is to select what Category. So, Combobox is for selecting Category from.
<TD>
<?php
$sql="SELECT categories.id as id, categories.name as name FROM categories";
$result=mysql_query($sql) or die(mysql_error());
$options="";
while ($row=mysql_fetch_assoc($result)) {
$id=$row["id"];
$thing=$row["name"];
$options.= " <OPTION VALUE=".$id.">".$thing.'</option>';
}
?>
<select name="CATEGORY" onClick=”submitCATEGORY();”>
<option value="0">Select Category
<?php echo $options;?></option>
</select>
</TD>
Now, I would like to have an EDIT FORM using the same script as for Inserting data in database using combobox.
<?php echo $CATEGORY; ?>
this script is for retrieving the data from database.
please help me to find a way, when I want to Edit a PRODUCT information to get combobox with option I selected during the inserting the data... I could succeed to fill the data for the name of products and other information, only Combobox is empty. I hope you could understand what I want to achieve! Thank you in advance for your time!!!
See bellow what I tried but did not succede:
<?php
$sql="SELECT categories.id as id, categories.name as name FROM categories";
$result=mysql_query($sql) or die(mysql_error());
$options="";
while ($row=mysql_fetch_assoc($result)) {
$id=$row["id"];
$thing=$row["name"];
$options.= " <OPTION VALUE=".$id.">".$thing.'</option>';
}
?>
<select name="CATEGORY" onClick=”submitCATEGORY();”>
<option value="<?php echo $CATEGORY; ?>">
<?php echo $options;?></option>
</select>
</TD>
Upvotes: 0
Views: 4956
Reputation: 635
Try the following in your edit page,
<?php
$CATEGORY = 3; //from DB table, consider 3 as category id for sample
$sql="SELECT categories.id as id, categories.name as name FROM categories";
$result=mysql_query($sql) or die(mysql_error());
$options="";
while ($row=mysql_fetch_assoc($result)) {
$id=$row["id"];
$thing=$row["name"];
$isSel = ($CATEGORY == $id)?"selected":'';
$options.= " <OPTION VALUE='$id' $isSel>$thing</option>';
}
?>
<select name="CATEGORY" onClick=”submitCATEGORY();”>
<option value="<?php echo $CATEGORY; ?>">
<?php echo $options;?></option>
</select>
</TD>
Upvotes: 1
Reputation: 89
Tryt this..If I understood corectly than:-
<option value="your_id" <?php echo $CATEGORY == your_id ?'selected':'';?>>your_category_name</option>
here $CATEGORY will be the retriving data from table
for your edit page you should do like this:-
<option value="1" <?php echo $CATEGORY == 1 ?'selected':'';?> ><?php echo $options;?></option>
Upvotes: 0
Reputation: 4593
If I understood you correctly then on Edit form you should mark the option that should be selected with 'selected ' tag:
<Option value="2" selected="selected">2</Option>
Upvotes: 0