doushai4890 2019-03-16 18:20
浏览 24
已采纳

搜索和维护价值观

I want to show the input field values and selected options after search. Here is my code:

<form action="" method="post">
    <input name="number" id="number" type="text" value="<?=$number?>">
    <select name="Country_Name" id="Country_Name">
        <option value="">Select</option>
<?php
    $sql = "SELECT * FROM country WHERE `is_deleted`=0";
    $query = mysqli_query($con,$sql);
    while($row = mysqli_fetch_array($query)):
?>  
        <option  value="<?=$row['country_value']?>" >
           <?=$row['Country_Name']?>
        </option>
<?php
    endwhile;
?>
    </select>
    <input type="submit" value="search" name="searchbtn">
</form>

The selected item will comes from database. The name of the table is country. And columns are: country_id, Country_Name, country_value

database columns picture

I have used this for keep the input field value:

 $number = (isset($_POST["number"])) ? $_POST["number"] : "";

But this format not working with selected options. How can I do that with php or javascript?

  • 写回答

1条回答 默认 最新

  • dragon201401 2019-03-16 18:51
    关注

    just need to set selected attribute for options.

    <form action="" method="post">
    <input name="number" id="number" type="text" value="<?=$number?>">
    <select name="Country_Name" id="Country_Name">
                    <option value="">Select</option>
                    <?php
                        $sql = "SELECT * FROM country WHERE `is_deleted`=0";
                        $query = mysqli_query($con,$sql);
                        while($row = mysqli_fetch_array($query)):
                        ?>  
                    <option  value="<?=$row['country_value']?>" <?=($country_selected ==$row['country_value']? "selected":"" )?> ><?=$row['Country_Name']?></option>
                    <?php
                        endwhile;
                        ?>
                    </select>
    

    values from post form

    $number = (isset($_POST["number"])) ? $_POST["number"] : "";
    $country_selected = (isset($_POST["Country_Name"])) ? $_POST["Country_Name"] : "";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度