单击转到按钮后如何保留选定的列表值? [英] how to hold a selected list value after clicking go button?

查看:43
本文介绍了单击转到按钮后如何保留选定的列表值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 php 页面,其中从 mysql 数据库填充下拉列表,当用户进行选择并单击开始"按钮时,有关该选定项目的数据将显示在页面上.现在我的页面中一切正常,但列表项名称会自动显示为默认值.例如:有 4 个列表项,特色艺术、流行艺术、自然艺术、幻想艺术.当用户选择pop art并按go按钮时,页面上会显示与此相关的数据,但下拉列表中不包含列表中选择的pop art名称.即使这样,下拉列表也会默认显示特色艺术.获取数据没有问题,但我想在下拉列表中显示作为选定的流行艺术或自然艺术或幻想艺术或特色艺术(不仅是特色艺术)以及它在页面上的相关数据等等.我想我的观点对所有人都很清楚.请帮帮我.

I have a php page where a dropdown list is populated from the mysql database and when a user make a selection and click 'go' button, data regarding that selected item will display on page. Now everything is working perfectly in my page but the list item names are automatically display as default. For example: there are 4 list items,Featured art, pop art, nature art, fantasy art. when user select pop art and press go button, the data related to this are displayed on page but the dropdown list does not hold pop art name as selected in list. Even then dropdown list shows Featured art as default. There is no problem of fetching data, but I want to display as selected pop art or nature art or fantasy art or featured art in dropdown list (not only the featured art)and its related data on page and so on. I think my point is clear to all. Help me please.

代码如下:

<select name="category">
<?php
$sql = "SELECT id, art_name FROM category;";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
 ?>
<option value="<?= $row['id']; ?>"><?= $row['art_name']; ?></option>

<?php

 }
 ?>
 </select>
 <input name="go" type="submit" value="Go" /></div>


 <div align="center" class="showimage">
 <ul class="display">

 <?php
  $id = (int)$_POST['category'];
 $sql_search = "SELECT id, categoryid, path FROM list WHERE categoryid = $id";
 $search = mysql_query($sql_search);
 $sql = mysql_query("SELECT autodisplay FROM list WHERE categoryid = 2");
 if (isset($_POST['go'])) {
 while ($row = mysql_fetch_assoc($search)) {
 ?>


<li><a href="<?= $row['path']; ?>" class="highslide" onClick="return hs.expand(this)"><img src="<?= $row['path']; ?>" border="0"></a></li>
<?php }

 }

 else {
 while ($row = mysql_fetch_assoc($sql)) {
 ?> 
 <li><a href="<?= $row['autodisplay']; ?>" class="highslide" onclick="return hs.expand(this)"><img src="<?= $row['autodisplay']; ?>" border="0"></a></li>
  <?php 
   }
   }
  ?>
 </ul>

这是运行页面

推荐答案

用以下内容替换您的选择列表:

Replace your select list with the following:

<select name="category">
    <?php
    $sql = "SELECT id, art_name FROM category;";
    $result = mysql_query($sql);
    while ($row = mysql_fetch_assoc($result)) {
        ?>

        <option value="<?= $row['id']; ?>"<?=($_POST['category']==$row['id'] ? ' selected="selected" : '')?>><?= $row['art_name']; ?></option>

        <?php
    }
    ?>
</select>

我添加了一个 if 语句来检查行 ID 是否与发布的变量匹配 - 如果匹配,则将选项值设置为 selected.

I've added an if statement to check if the row id matches the posted variable - if so set the option value to selected.

这篇关于单击转到按钮后如何保留选定的列表值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆