将单选按钮设置为会话以保持已检查状态

时间:2017-03-01 09:20:26

标签: php session

我正在尝试存储单选按钮的输入值并将其存储到会话中,这样如果用户在网站周围漫游,收音机将保持检查状态,除非他们自己切换,然后新选择仍保持检查。

<form action="" method="POST" id="reportSwitch">
    <input checked type="radio" name="reportType" id="leadership" value="1" <?php if($reportType == 1){
        echo 'checked';} ?>>
    <label for="leadership">Leadership</label>
    <input type="radio" name="reportType" id="fundementals" value="2" <?php if($reportType == 2){
        echo 'checked';} ?>>    
    <label for="fundementals">Fundementals</label>
</form>


<?php
$_SESSION['reportType'] = $_POST['reportType'];
$reportType = $_SESSION['reportType'];

if(isset($reportType)){
} else{
    $reportType = 1;
}
?>

我似乎无法让它保持在检查状态......

2 个答案:

答案 0 :(得分:1)

检查此代码

<?php
session_start();
$_POST['reportType'] = 1; // for testing it is set define value , you can change 
if(isset($_POST['reportType'])){
 $_SESSION['reportType'] = $_POST['reportType'];
 $reportType = $_SESSION['reportType'];
} else {
 $reportType = $_SESSION['reportType'];
}

 if(!isset($reportType)){
 $reportType = 1;
 }
?>
<form action="" method="POST" id="reportSwitch">
<input checked type="radio" name="reportType" id="leadership" value="1" <?php if($reportType == 1){
    echo 'checked';} ?>>
<label for="leadership">Leadership</label>
<input type="radio" name="reportType" id="fundementals" value="2" <?php if($reportType == 2){
    echo 'checked';} ?>>
<label for="fundementals">Fundementals</label>
 </form>

答案 1 :(得分:1)

将值放入会话中并使用会话变量填充单选按钮中的值,而不是使用额外的变量。

从会话中填充它将有助于保留所有页面。

<?php
$_SESSION['reportType'] = $_POST['reportType'];
?>
<form action="" method="POST" id="reportSwitch">
    <input type="radio" name="reportType" id="leadership" value="1" <?php if($_SESSION['reportType'] == 1){
        echo 'checked';} ?>>
    <label for="leadership">Leadership</label>
    <input type="radio" name="reportType" id="fundementals" value="2" <?php if($_SESSION['reportType'] == 2){
        echo 'checked';} ?>>    
    <label for="fundementals">Fundementals</label>
</form>