Html选择标签问题

时间:2012-04-08 05:37:29

标签: php

我的表格中有两个字段。

1)Html选择字段项(来自mysql数据库)
2)名称框。

如果用户从html选择标签中选择一个项目并忘记写下他/她的名字,那么用php就可以了..

* Your username required

但是它没有显示用户选择的选择项目..它再次显示选择... 这就是为什么会出现另一条错误消息..

* Please select your item.

如何使用php显示此选定项目?

Html格式:

<tr>    
<td>Class</td>
<td>
<select class="td" name="class">
    <option selected="selected" value="">Select..</option>
    <?php
    include_once("../../toplevel/content/manage/dbcon/dbcon.php");
    $sql = mysql_query("SELECT * FROM class");
    while($re = mysql_fetch_array($sql))
    {
$re_class = mysql_real_escape_string(trim($re['c_name']));      
        echo "<option value='$re_class'> $re_class </option>";
    }
    ?>  
</select>
</td>
</tr>

<tr>
<td>Name</td>
<td><input type="text" value="<?php if(isset($_POST['uname'])) echo 
$_POST['uname']; ?>" name="uname" class="td" /></td>
</tr>
<tr>

我可以为Name提交这个但是我如何为select字段执行此操作?
非常感谢。
Shibbir

3 个答案:

答案 0 :(得分:0)

  <tr>    
  <td>Class</td>
<td>
<select class="td" name="class" onchange="this.form.submit();">
<option selected="selected" value="">Select..</option>
<?php
include_once("../../toplevel/content/manage/dbcon/dbcon.php");
$sql = mysql_query("SELECT * FROM class");
while($re = mysql_fetch_array($sql))
{
$re_class[] = mysql_real_escape_string(trim($re['c_name']));      

}
?>  
<?php for($i = 0; $i < count($re_class); $i++) { ?>

<?php echo "<option value='$re_class[$i]'";
if($re_class[$i] == $_POST['class']) { echo "selected=selected"; } ?> <?php echo ">" . $re_class[$i] . </option>"; ?>

<?php } ?>
   </select>
   </td>
   </tr>

 <tr>
 <td>Name</td>
 <td><input type="text" value="<?php if(isset($_POST['uname'])) echo 
     $_POST['uname']; ?>" name="uname" class="td" /></td>
 </tr>
 <tr>

修改如果当前$_POST变量等于$re_class数组中的选项,您似乎希望选择选项值。

答案 1 :(得分:0)

<tr>    
<td>Class</td>
<td>
<select class="td" name="class">
    <option value="">Select..</option>
    <?php
    include_once("../../toplevel/content/manage/dbcon/dbcon.php");
    $sql = mysql_query("SELECT * FROM class");
    while($re = mysql_fetch_array($sql))
    {
        $re_class = mysql_real_escape_string(trim($re['c_name']));
        if(isset($_POST['class'])){
            echo '<option '.($_POST['class']==$re_class?'selected':'').' value='.$re_class.'>'.$re_class.'</option>';
        }else{
            echo '<option value='.$re_class.'>'.$re_class.'</option>';
        }
    }
    ?>  
</select>
</td>
</tr>

<tr>
<td>Name</td>
<td><input type="text" value="<?php if(isset($_POST['uname'])) echo 
$_POST['uname']; ?>" name="uname" class="td" /></td>
</tr>
<tr>

答案 2 :(得分:0)

    <tr>    
    <td>Class</td>
    <td>
    <select class="td" name="class">
        <option value="">Select..</option>
        <?php
        include_once("../../toplevel/content/manage/dbcon/dbcon.php");
        $sql = mysql_query("SELECT * FROM class");
        while($re = mysql_fetch_array($sql))
        {
           $re_class = mysql_real_escape_string(trim($re['c_name'])); 
           $sel='';
           if($re_class==$_POST['class'])  {
             $sel='selected="selected"';
           }   
            echo "<option value='$re_class' ".$sel."> $re_class </option>";
        }
        ?>  
    </select>
    </td>
    </tr>

    <tr>
    <td>Name</td>
    <td><input type="text" value="<?php if(isset($_POST['uname'])) echo 
    $_POST['uname']; ?>" name="uname" class="td" /></td>
    </tr>
    <tr>
你是说这个吗?