duanji2002 2010-10-14 08: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 08: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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部