duanji2002 2010-10-14 16:25
浏览 50
已采纳

在表单操作中使用PHP_SELF在标头重定向上丢失了PHP会话变量

I have a multi-step form, let's say for the sake of ease it's 2 steps. First step I want to select a radio button and based on that radio button selection it takes me to a certain page, but I also want that selection stored in a session. I have 2 pages: page1.php

session_start();

if(isset($_POST['post'])) {
if (($_POST['country'] == 'US')) {
header("Location: US_Products.php"); }
elseif (($_POST['country'] == 'CDN')) {
header("Location: CDN_Products.php"); }
else { die("Error"); }
exit;
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="USA">USA:</label>
<input type="radio" name="country" value="US">
<label for="CDN">Canada:</label>
<input type="radio" name="country" value="CDN">
<input type="submit" name="post" value="Go To Filter">
</form>

Page2.php (either A or B)

session_start();
$_SESSION['country'] = $_POST['country'];
<?php echo $_SESSION['country']; ?>

The Country choice is not being passed when I have it do this conditional redirect. Is there a problem with session variables and redirects or session variables and PHP_SELF or something?

  • 写回答

2条回答 默认 最新

  • du155305 2010-10-14 16:52
    关注

    Page 1:

    session_start();
    
    if(isset($_POST['post'])) {
        $_SESSION['country'] = $_POST['country'];
        if (($_POST['country'] == 'US')) {
            header("Location: US_Products.php"); }
        elseif (($_POST['country'] == 'CDN')) {
            header("Location: CDN_Products.php"); }
        else { die("Error"); }
        exit;
    }
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label for="USA">USA:</label>
    <input type="radio" name="country" value="US">
    <label for="CDN">Canada:</label>
    <input type="radio" name="country" value="CDN">
    <input type="submit" name="post" value="Go To Filter">
    </form>
    

    Page 2:

    session_start();
    <?php echo $_SESSION['country']; ?>
    

    Or using include method, just use one page:

    session_start();
    
    if(isset($_POST['post'])) {
        if (($_POST['country'] == 'US')) {
            include("US_Products.php"); }
        elseif (($_POST['country'] == 'CDN')) {
            include("CDN_Products.php"); }
        else { die("Error"); }
        exit;
    }
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label for="USA">USA:</label>
    <input type="radio" name="country" value="US">
    <label for="CDN">Canada:</label>
    <input type="radio" name="country" value="CDN">
    <input type="submit" name="post" value="Go To Filter">
    </form>
    

    and you should be able to use echo $_POST['country'] on US_Products.php and CDN_Products.php, or

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路