简体   繁体   English

PHP下拉框未显示所选值

[英]PHP drop down box not showing selected value

Hi I have a piece of php code which takes php variables from a database and then populates A drop down box. 嗨,我有一段PHP代码,它从数据库中获取php变量,然后填充A下拉框。 This works fine but once the page updates it doesn't display the selected value and instead just displays the original list again. 这工作正常,但一旦页面更新,它不会显示所选的值,而只是再次显示原始列表。

I have played around with some different ideas of using isset to displays the selected value, but then it just displays that value and nothing else. 我已经玩过使用isset来显示所选值的一些不同想法,但它只是显示该值而没有别的。

I have attached the original code where it works but always only shows the original list of values from the database. 我已将原始代码附加到其工作的位置,但始终只显示数据库中的原始值列表。

If any body could provide a solution or even a nudge in the right direction then it would be much appreciated. 如果任何身体可以提供解决方案,甚至可以在正确的方向上轻推,那么我们将非常感激。

Thanks 谢谢

if (isset($select) && $select != "location") {
    $select = $_POST['location'];
}
?>

<select name="location">
<?php
// Get records from database (table "name_list").
$list = mysql_query("select DISTINCT region_name from masterip_details WHERE country_code='GB' AND TRIM(IFNULL(region_name,'')) <> '' order by region_name asc");

// Show records by while loop.
while ($row_list = mysql_fetch_assoc($list)) {
    $location = $_POST['location']; ?>
    <option value="<?php echo $row_list['region_name']; ?>"
    <?php if  $row_list['region_name'] == $select) {
        echo "selected";
    } ?>><?php echo $row_list['region_name']; ?></option>        
<?php } ?>

You are sure you send that form with a post request? 您确定要发送带有帖子请求的表单吗?

Your code is unreadable Better do something like this: 你的代码是不可读的最好做这样的事情:

<?php

    //database request don't belong in the presentation layer
    $list=mysql_query("select DISTINCT region_name from masterip_details WHERE country_code='GB' AND TRIM(IFNULL(region_name,'')) <> '' order by region_name asc");
    $location = "";
    $isSelect = "";
    if(isset($_POST['location'])){
        $location = $_POST['location']; 
    }
    while($row_list=mysql_fetch_assoc($list)){
        if($row_list['region_name'] == $location ){ 
            $isSelect = "selected"; 
         } 
         echo '<option value="'.$row_list['region_name'].'" '.$isSelect.'  >'. $row_list['region_name'].'</option>';

    }
?>

You are not checking the select value properly. 您没有正确检查选择值。

Change this: 改变这个:

if (isset($select) && $select != "location") {
    $select = $_POST['location'];
}

TO

if (isset($_POST['location']) && $_POST['location'] != "location") {
    $select = $_POST['location'];
}

While creating the <option> tag, you may use the following: 在创建<option>标记时,您可以使用以下内容:

<?php if ($row_list['region_name'] == $select) { echo "selected='true'"; } ?>

You may also like to check if the condition $row_list['region_name'] == $select is returning true . 您可能还想检查条件$row_list['region_name'] == $select是否返回true You may try to echo their result separately as: 您可以尝试单独echo他们的结果:

echo ($row_list['region_name'] == $select)

or something similar that would show if you get true or not. 或类似的东西,如果你的true与否。

问题出在第一行 - 实际上系统永远不会执行第二行来分配$select因为$select永远不会被设置:

if(isset($_POST['select']) && $select!="location") { $select=$_POST['location']; } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM