开发者

set default drop down value on php generated form

I generated a drop down menu with the code below. How can I set the field to the GET variable after submit is clicked?

<select>

<?php
    $sql="SELECT c FROM prob开发者_运维百科lems WHERE b='$id'"; 
    $result=mysql_query($sql); 

    $options=""; 

    while ($row=mysql_fetch_array($result)) { 

    $id=$row["c"]; 
    $c=$row["c"]; 
    $options.="<option value=\"$id\">".$c; 
    } 
?>
        <option value=''>C <?=$options?>  </option>     
</select>


<select>

<?php
    $sql="SELECT c FROM problems WHERE b='$id'"; 
    $result=mysql_query($sql); 

    $options=""; 

    while ($row=mysql_fetch_array($result)) { 

    $id=$row["c"]; 
    $c=$row["c"]; 
    if($_GET['var'] == $id)
      echo '<option selected="selected" value=\"$id\">' . $c . '</option>';
    else
       echo '<option value=\"$id\">' . $c . '</option>'; 
    } 
?>

</select>

Basic idea is compare value GET data with database data and using if else condition add selected="selected" if condition matched. I am directly printing string as they will not be getting use later on.


I'm a bit confused about what you want to know. To have an option selected by default, use the selected="selected" attribute on that option.

If you want to know how to get the submitted value in PHP, you need to assign the name attribute to the <select> tag. In general, however, you've got the idea right.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜