简体   繁体   English

使用PHP根据会话变量选择下拉选项

[英]Selecting Dropdown Option Based on Session Variable Using PHP

I am using PHP to populate my options for a dropdown menu in HTML via Javascript append. 我正在使用PHP通过Javascript附加来填充HTML下拉菜单的选项。 It works so far, and I am using a conditional statement to only show the value for that team ID if the session variable is set. 到目前为止,它仍然有效,如果设置了会话变量,我将使用条件语句仅显示该团队ID的值。 The issue is i need to display all the dropdown options (like the else/while statement in PHP) even when the session variable is set so people can still have that option to manually select another ID, but if session is set auto select the value that the session variable is set to.... otherwise just display the options selecting default option (1) like in my else statement. 问题是即使设置了会话变量,我也需要显示所有下拉选项(如PHP中的else / while语句),这样人们仍然可以使用该选项来手动选择另一个ID,但是如果设置了会话,则自动选择该值会话变量设置为...。否则,仅显示选择默认选项(1)的选项,就像我的else语句中一样。 Any suggestion on how I should go about this? 关于我应该如何处理的任何建议? Thanks! 谢谢!

HTML 的HTML

<td class="label_container">Team ID#</td>
            <td class="input_container">
            <select id="prepping_team" name="prepping_team"></select>
            </td>

JS JS

//Populate Team IDs
function get_prepping_team() {
$.ajax({
    url: './php/getprepping_team.php',
    success: function(data) {
        $('#prepping_team').append(data);
    }
});
};

PHP 的PHP

//Grab Session (if set)
session_start();
$_SESSION['prepping_team'];

$prep_team = $_POST['prepping_team'];

header("Cache-Control: no-cache, must-revalidate");
header('Access-Control-Allow-Origin: *');
header('Content-type: multipart/form-data');

$logAuditFile = "../log/prepform.log";
$logModule = "getprepping_team.php";
include '/mnt/library/auditlog.php';

require("/mnt/library/configdb.php");        
include '/mnt/library/accessdb.php';

//Parse and store the db ini file, this will return an associative array
db_config_cables_test();
db_connect();

$sql = mysql_query("SELECT pt_id FROM preppingteam;");
//IF SESISON VARIALBE IS SET
if(isset($_SESSION['prepping_team'])) {
    print "<option value=\"".$_SESSION['prepping_team']."\">".$_SESSION['prepping_team']."</option>";
    //print "<option value=\"".$_SESSION['prepping_team']."\">".$_SESSION['prepping_team']."</option>";
//OTHERWISE LOAD TEAM IDs
} else {
    while ($data = mysql_fetch_assoc($sql)) {
        print "<option value=\"".$data['pt_id']."\">".$data['pt_id']."</option>";
    }
}

last part of the code is not doing what you want. 代码的最后一部分没有做您想要的。 So correct it like this 所以像这样更正

Update: 更新:

while ($data = mysql_fetch_assoc($sql)) {
   if(isset($_SESSION['prepping_team']) && $_SESSION['prepping_team'] == $data['pt_id'])
    {
    print "<option selected = 'selected'  value=\"".$_SESSION['prepping_team']."\">".$_SESSION['prepping_team']."</option>";    
    }
   else
    print "<option value=\"".$data['pt_id']."\">".$data['pt_id']."</option>";
}

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

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