PHP:如何在3页之间传递会话变量?

时间:2018-04-18 15:25:30

标签: php session post get session-variables

到目前为止,我可以使用post和get成功地将结果从第一页移到第二页,但无论我做什么都不会将信息移到第3页。我试图将它切换到会话后阅读它完全为此做了但由于某种原因我做错了,经过几个小时的搜索我不知道我的生活弄清楚它是什么。我已经关注了本网站上有关该主题的指南,关注视频和其他帖子。我现在得出结论,这只是我,我需要一些帮助。任何帮助将不胜感激。

第1页(索引页|输入变量):

<?php session_start();
$_GET['q'] = $q;
$_GET['s'] = $s;
?>

<form action="search.php" method="get">
<input name="q" maxlength="8" type="text" placeholder="License Plate" id="textbox" required />
<select name="s" id="s" required aria-required="true">
<option value="" disabled selected>CHOOSE STATE</option>
<option value="AL">ALABAMA</option>
<option value="AK">ALASKA</option>
<option value="AZ">ARIZONA</option>
<option value="AR">ARKANSAS</option>
<option value="CA">CALIFORNIA</option>
<option value="CO">COLORADO</option>
<option value="CT">CONNECTICUT</option>
etc...
</select>
<input type="submit" value="SEARCH" id="submitbtn"></form>

第2页(Search.php,如果已经创建,将直接转到指定的页面):

<?php session_start();
$q = $_POST['q'];
$s = $_POST['s'];
?>
<?php
$dir = 'states';
$s = (isset($_GET['s']))? strtolower($_POST['s']) : '';
$q = (isset($_GET['q']))? strtoupper($_POST['q']) : ''; 
$res = opendir($dir); 

while(false!== ($file = readdir($res))) {
    if(strpos(strtoupper($file),$q)!== false &&!in_array($file)) {
        echo "<a href='$dir/$s/$q.htm'>$file</a>";
    }
}

closedir($res);
?>

<?php
echo $htmlHeader;
while($stuff){
echo $stuff;
}
echo "<script>window.location = 
'http://www.somesite.com/$dir/$s/$q.htm'</script>";
 ?>

第3页(404页面用于捕获系统中没有的所有内容):

<?php session_start();
?>
<form action="" method="" name="FormChoice">
<input name="q" maxlength="8" type="text" value="<?php echo $_POST['q']; ?>" id="q" required>
<select name="s" id="s" required aria-required="true">
<option value="" disabled>CHOOSE STATE</option>
<option value="AL" <?php if($_POST['s'] == al) {echo ' selected="selected"';} ?>>ALABAMA</option>
<option value="AK" <?php if($_POST['s'] == ak) {echo ' selected="selected"';} ?>>ALASKA</option>
<option value="AZ" <?php if($_POST['s'] == az) {echo ' selected="selected"';} ?>>ARIZONA</option>
<option value="AR" <?php if($_POST['s'] == ar) {echo ' selected="selected"';} ?>>ARKANSAS</option>
<option value="CA" <?php if($_POST['s'] == ca) {echo ' selected="selected"';} ?>>CALIFORNIA</option>
<option value="CO" <?php if($_POST['s'] == co) {echo ' selected="selected"';} ?>>COLORADO</option>
<option value="CT" <?php if($_POST['s'] == ct) {echo ' selected="selected"';} ?>>CONNECTICUT</option>
</select>
<input type="submit" id="submitbtn2" value="SEARCH" name="submit" OnClick="search()" />
<span id="or">OR</span>
<input type="submit" id="addbtn" value="ADD" name="submit" OnClick="add()" /> 
</form>

1 个答案:

答案 0 :(得分:1)

1页

<?php 
session_start();
// next 2 lines do NOTHING remove them
// as you have not yet loaded any values into $q and $s
//$_GET['q'] = $q;
//$_GET['s'] = $s;
?>

<form action="search.php" method="get">
<input name="q" maxlength="8" type="text" placeholder="License Plate" id="textbox" required />
<select name="s" id="s" required aria-required="true">
<option value="" disabled selected>CHOOSE STATE</option>
<option value="AL">ALABAMA</option>
<option value="AK">ALASKA</option>
<option value="AZ">ARIZONA</option>
<option value="AR">ARKANSAS</option>
<option value="CA">CALIFORNIA</option>
<option value="CO">COLORADO</option>
<option value="CT">CONNECTICUT</option>
etc...
</select>
<input type="submit" value="SEARCH" id="submitbtn"></form>

第2页 - 搜索 - 从以前的表单接收数据   - 包含许多不必要的<?php...?>   - 上一个表单使用method="get",因此数据将到达$_GET数组而不是$ _POST数组

<?php 
session_start();
//$q = $_POST['q'];
//$s = $_POST['s'];

// But this is silly as you have not yet tested these values exist
// but you do that in the next lines
//$q = $_GET['q'];
//$s = $_GET['s'];

$dir = 'states';
$s = (isset($_GET['s']))? strtolower($_POST['s']) : '';
$q = (isset($_GET['q']))? strtoupper($_POST['q']) : ''; 
$res = opendir($dir); 

// Now if you want to pass the values of `q` and `s` on to the next form
// they now need to be added to the session
$_SESSION['q'] = $q;
$_SESSION['s'] = $s;

while(false!== ($file = readdir($res))) {
    if(strpos(strtoupper($file),$q)!== false &&!in_array($file)) {
        echo "<a href='$dir/$s/$q.htm'>$file</a>";
    }
}

closedir($res);

echo $htmlHeader;
while($stuff){
    echo $stuff;
}
echo "<script>
        window.location = 'http://www.somesite.com/$dir/$s/$q.htm';
      </script>";
// added missing semi colon                                       ^
?>

第3页(404页面用于捕获系统中没有的所有内容):

现在,当你到达这个页面时,数据将在SESSION中可用。