如何根据登录用户设置下拉列表默认选择

时间:2013-05-30 13:48:48

标签: php jquery sql drop-down-menu

我有一个下拉控件,我希望它根据登录用户的访问权限默认为特定选项。例如,下拉列表有10个选项,但只有管理员才有权查看所有10个选项。大多数用户只能访问其中一个选项。根据下拉列表的值是否为空来隐藏/显示页面内容。

问题:使用上面的示例,如果管理员已登录,我需要将下拉列表默认为“选择一个选项”。这样就隐藏了页面内容。另一方面,如果只有1个访问权限的用户登录,我需要将其默认为1.这样他们就不必选择任何内容,默认情况下会显示页面内容。我该怎么做呢?

下面是我当前的代码,它根据选择的时间来处理下拉列表的显示内容。

PHP / HTML

// Hide/Show main content div 
<?php
if (isset($_GET['src'])) {
    $src = $_GET['src'];
} else {
?>
<style>
#divmain { display: none; }
</style>
}
<?php } ?>

// Start of form, header, etc.
<select name="select1" id="select1">
<?php
$sql = getOptions();
$data = makeConnection($sql);
if ($src == null) {    // If value is null, default to 'Select an option'
    echo "<option selected value=\"\" disabled=\"disabled\">--Select an option--</option>";
    while ($row = odbc_fetch_array($db)) {
        echo "<option value=\"".$row['content']."\">".$row['content']."</option>";
    }
} else {    // If value not null keep the selected value selected
    while ($row = odbc_fetch_array($db)) {
        if ($row['content'] == $src) { $selected = " selected "; }
        else { $selected = " "; }
        echo "<option value=\"".$row['content']."\" ".$selected.">".$row['content']."</option>";
    }
}
?>
</select>


JS

// Pass selected value on change
$('#select1').change(function() {
    var sel = $(this).val();
    location.href = "page1.php?src=" + sel;
}


SQL

// Hardcoding user for testing purposes, THIS WILL BE CHANGED
function getOptions() {
    $results = "SELECT content, userid FROM table WHERE userid = 'username'";

    return $results;
}


非常感谢任何帮助,如果我不清楚任何事情,请告诉我。

2 个答案:

答案 0 :(得分:1)

得到了一些帮助,现在就弄明白了。这是修改后的代码:

<强> PHP / HTML

// Hide/Show main content div 
<?php
$src = null;
if (isset($_GET['src'])) {
    $src = $_GET['src'];
} else {
?>
<style>
    #divmain { display: none; }
</style>
}
<?php } ?>

// Start of form, header, etc.
<select name="select1" id="select1">
<?php
$sql = getOptions();
$data = makeConnection($sql);
if ($src == null) {
    $i = 0;
    $content = "";
    while ($row = odbc_fetch_array($db)) {
        $content .= "<option value=\"".$row['content']."\">".$row['content']."</option>";
        $i++;
    }

    if ($i > 1) {
        echo "<option selected value=\"\" disabled=\"disabled\">--Select an option--</option>";
    }
    echo $content;

    if ($i > 1) { $oneopt = 1; }
    else { $oneopt = 0; }
} else {
    while ($row = odbc_fetch_array($db)) {
        if ($row['content'] == $src) { $selected = " selected "; }
        else { $selected = " "; }
        echo "<option value=\"".$row['content']."\" ".$selected.">".$row['content']."</option>";
    }
}
?>
</select>


JS

$('#select1').change(function() {
    var sel = $(this).val();
    location.href = "page1.php?src=" + sel;
}

<?php
    global $optone;
    if ($optone == 1) {
        echo "$('#select1').trigger('change');";
    }
?>


SQL - 保持相同

@chenasraf非常感谢帮助!希望这对未来的某些人有所帮助!

答案 1 :(得分:0)

预先选择的选项始终是第一个选择&#34;#34;属性。您的第一个残疾人在所有情况下都首先使用它,所以它总是被选中。删除它并从那里工作。

另一方面,我认为您可以设法让这些选项部分更好地运作。我已经冒昧为你重写了它:

<select name="select1" id="select1">
<?php
    $selected = '';
    while ($row = obdc_fetch_array($db)) {
        $options[] = '<option value="'.$row['content'].'">'.$row['content'].'</option>';
        if ($row['content'] == $src)
            $selected = count($options) - 1;
    }
    array_unshift($options, '<option disabled="disabled"', $selected == '' ? ' selected="selected"' : '','>--Select an option--</option>');

    foreach ($options as $option) {
        echo $option;
    }
?>
</select>