简体   繁体   English

PHP-动态填充的选择下拉列表中的pre select / save选项

[英]PHP - pre select/save option on dynamically populated select dropdowns

I have a dropdown box that holds the days 1-31 and i want to store/save what the user has previously selected if they return to the page. 我有一个保存第1-31天的下拉框,如果他们返回页面,我想存储/保存用户先前选择的内容。

My function to generate the box is: 我生成盒子的功能是:

public function fetchDDMMYYYYDropdown($select_d,$session_d) {
        $days = range (1, 31);
        $dropdown .=  '<select name="'.$select_d.'">';
        foreach($days as $key=>$name){
            if($session_d==$name){
                $session = 'selected';
            }
            $dropdown .= '<option value="'.sprintf("%02d", $name).'" selected="'.$session.'">'.sprintf("%02d", $name).'</option>';
        }
        $dropdown .=  '</select>';

        return $dropdown;
    }

And my form is on this page: 我的表格在此页面上:

<?php
session_start();
include("includes/func.class.php");

$dob = $func->fetchDDMMYYYYDropdown('dob_d', $_SESSION['dob_d']);
?>
<form action="t35t_send.php" method="get">
<?php echo $dob;?>
<input type="submit" value="send">
</form>

And it goes to this to save the SESSION variable: 然后保存SESSION变量:

session_start();
$_SESSION['dob_d'] = $_GET['dob_d'];
$dob = $_SESSION['dob_d'];
echo $dob;

I can tell that the $_SESSION['dob_d'] is correct and saved as i can output it inside both the function and the initial form page - so it's just the following which must not be right but at the moment the dropdown box just resets back to the first value, not the saved session: 我可以告诉您$ _SESSION ['dob_d']是正确的并已保存,因为我可以在函数和初始表单页面中将其输出-因此,以下内容可能不正确,但下拉列表刚刚重置返回第一个值,而不是保存的会话:

if($session_d==$name){
    $session = 'selected';
}
$dropdown .= '<option value="'.sprintf("%02d", $name).'" selected="'.$session.'">'.sprintf("%02d", $name).'</option>';

try this 尝试这个

function fetchDDMMYYYYDropdown($select_d,$session_d) {

    $days = range (1, 31);
    $dropdown .=  '<select name="'.$select_d.'">';
    foreach($days as $key=>$name){
        if($session_d==$name){

            $session = 'selected = selected';
        }
        else
        {
            $session = '';
        }
        $dropdown .= '<option value="'.sprintf("%02d", $name).'"'.$session.'">'.sprintf("%02d", $name).'</option>';
    }
    $dropdown .=  '</select>';

    return $dropdown;
}

The Problem was if even 'selected' is there in "option" even then value gets selected and in your previous code .. 'selected will be there for every date .. so it eas showing '31'. 问题是,即使在“选项”中甚至有“已选择”,也就选择了值,并且在您以前的代码中..“已选择在每个日期都将存在。”因此它很容易显示“ 31”。 I have changed the code so that 'selected = selected ' gets echo for the saved value. 我更改了代码,以使“ selected = selected”得到回显以保存值。

Hope it helps you 希望对您有帮助

A rough guess your if condition is not returning true, 粗略地猜测您的条件是否不为真,

Try checking the value of both variables. 尝试检查两个变量的值。

Just to confirm can you replace the code as below and try. 只是为了确认您是否可以替换下面的代码并尝试。

if($session_d == sprintf("%02d", $name)){
    $session = 'selected';
}
$dropdown .= '<option value="'.sprintf("%02d", $name).'" selected="'.$session.'">'.sprintf("%02d", $name).'</option>';

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

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