Php if-else和session-如何停止if';她来了?
我不确定我在这里做错了什么,但在从第一个“if unset”if下拉列表中进行选择后,该下拉列表仍然显示在新if的输出上方 选择后,应转到“ukset”或“uknotset”,不再显示下拉列表 知道我做错了什么吗Php if-else和session-如何停止if';她来了?,php,Php,我不确定我在这里做错了什么,但在从第一个“if unset”if下拉列表中进行选择后,该下拉列表仍然显示在新if的输出上方 选择后,应转到“ukset”或“uknotset”,不再显示下拉列表 知道我做错了什么吗 <?php session_start(); // if unset if(!isset($_SESSION['grant_access'])) {$_SESSION['grant_access'] = 'unset';}
<?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';
}
?>
这是因为您更改会话值的顺序。首先,将其设置为
unset
,并显示表单。然后,在表单提交时,您的代码运行,但由于会话变量仍然是unset
表单再次可见
将您的$\u POST
检查放在代码开头,例如:
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// post check, change session value
}
// rest of the code
您应该在选择框中执行一次更改。检查下面的代码
<?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>';
}
您是否检查了我提供的代码caz我添加了您忘记添加的表单id和名称。我检查了它,它的工作情况您可以使用我的代码进行检查。谢谢,我知道这会很简单!