dongtan2017 2013-04-01 16:29
浏览 45
已采纳

使用PHP选择基于会话变量的下拉选项

I am using PHP to populate my options for a dropdown menu in HTML via Javascript append. 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. 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. Any suggestion on how I should go about this? Thanks!

HTML

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

JS

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

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>";
    }
}
  • 写回答

1条回答 默认 最新

  • du4822 2013-04-01 16:39
    关注

    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>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比