简体   繁体   中英

How to populate a combo-box in EDIT FORM

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; ?> <?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>

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>

如果我对您的理解正确,则应在“编辑”表单上标记“ selected”标记应选择的选项:

<Option value="2" selected="selected">2</Option>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM