通过选择下拉菜单选项取消设置会话

时间:2017-04-14 08:55:56

标签: php jquery session

我想在我的下拉菜单中添加一个选项,用于取消设置或销毁会话$_SESSION['selected_opt]

练习代码:

<form class="search-form">
    <select name="thema" class="selectpicker">
        <option value="t1" class="special">Select something</option>
            <optgroup label="Select...">
                <option value="s1" data-icon="glyphicon-sort-by-alphabet" class="special">Selection 1</option>
                <option value="s1" data-icon="glyphicon-sort-by-alphabet" class="special">Selection 2</option>
                <option value="s1" data-icon="glyphicon-sort-by-alphabet" class="special">Selection 3</option>
                <option value="s4" data-icon="glyphicon-sort-by-alphabet" class="special">Selection 1</option>
            </optgroup>
    </select>
</form>

我见过这样的代码:

<option value="black" <?php if(isset($_SESSION['kategorie']) == "black") { echo ' selected';} ?>>Black and white</option>

所以我在代码中加了类似的东西:

<option value="t1" <?php if($_SESSION['selected_opt']) { unset($_SESSION['selected_opt']);} ?> class="class_opt">Select something</option>

但当然这不起作用。这是可能的还是我应该使用像jQuery这样的东西?我可以在任何地方找到合适的答案......我希望有人可以提供帮助...

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

<select name="thema" class="selectpicker" change='selectChanged(this);'>

并创建函数:

<script>
function selectChanged(obj){
    if($(obj).val() == "LogOut"){
         //redirect to your session clearing / log out page
         window.location.href = 'http://example.com/LogOut.php''
    };
});
</script>

您的选择将是:

<option value="LogOut">Log Out</option>