0

I'm not sure what I'm doing wrong here, but after a selection is made in the dropdown from the first "if unset" if, the dropdown still shows up above the the new if's output.

Once a selection is made, it should go to "ukset" or "uknotset" and not show the dropdown again.

Any idea what I am doing wrong?

<?php
        session_start();

        // if unset
        if(!isset($_SESSION['grant_access'])) {$_SESSION['grant_access'] = 'unset';}
        if($_SESSION['grant_access'] == 'unset') {
            echo '<p>Make your choice</p>';
            echo '<form action="" onchange="this.submit()" method="post">';
            echo '<select name="thecountry">';
                echo '<option>Choose</option>';
                echo '<option value="hello">Hello</option>';
                echo '<option value="goodbye">Goodbye</option>';
                echo '<option value="unsure">Unsure</option>';
            echo '</select>';
            echo '</form>';
            }

        // if UK is set
        if(isset($_POST['thecountry']) && ($_POST['thecountry'] == 'hello')) {$_SESSION['grant_access'] = 'ukset';}
        if($_SESSION['grant_access'] == 'ukset') {
            echo 'welcome';
            }

        // if UK is not set
        if(isset($_POST['thecountry']) && ($_POST['thecountry'] !== 'hello')) {$_SESSION['grant_access'] = 'uknotset';}
        if($_SESSION['grant_access'] == 'uknotset') {
            echo 'access denied';
            }   

?>
4

2 回答 2

1

那是因为您更改会话值的顺序。首先,您将其设置为unset并显示表格。然后,在表单提交时,您的代码运行,但由于会话变量仍然是unset表单再次可见。

将您的$_POST检查放在代码的开头,例如:

if($_SERVER['REQUEST_METHOD'] == 'POST') {
 // post check, change session value
}

// rest of the code
于 2013-05-23T12:36:05.300 回答
1

你应该在选择框上做onchange 。检查下面的代码

<?php
    session_start();
    // if UK is set
    if(isset($_POST['thecountry']) && ($_POST['thecountry'] == 'hello')) {$_SESSION['grant_access'] = 'ukset';}
    if(@$_SESSION['grant_access'] == 'ukset') {
        echo 'welcome';
        }

                // if UK is not set
    else if(isset($_POST['thecountry']) && ($_POST['thecountry'] !== 'hello')) {$_SESSION['grant_access'] = 'uknotset';}
    if(@$_SESSION['grant_access'] == 'uknotset') {
        echo 'access denied';
        }   


    // if unset
    else if(!isset($_SESSION['grant_access'])) {$_SESSION['grant_access'] = 'unset';}
    if($_SESSION['grant_access'] == 'unset') {
        echo '<p>Make your choice</p>';
        echo '<form action="" name="frm" id="frm"  method="post">';
        echo '<select name="thecountry" onchange="frm.submit()">';
            echo '<option>Choose</option>';
            echo '<option value="hello">Hello</option>';
            echo '<option value="goodbye">Goodbye</option>';
            echo '<option value="unsure">Unsure</option>';
        echo '</select>';
        echo '</form>';
        }

?>

于 2013-05-23T12:40:47.730 回答