0

我的表单中有两个字段。

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 字段执行此操作?
非常感谢。
希比尔

4

3 回答 3

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数组中的选项,您似乎希望选择选项值。

于 2012-04-08T05:43:45.693 回答
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>
于 2012-04-08T05:55:57.323 回答
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>

你是这个意思吗?

于 2012-04-08T05:59:01.653 回答