使用PHP会话时在第二页上获取未定义的变量

时间:2018-08-16 13:14:03

标签: php html session

我正在处理会话当我从第1页到第2页提交表单时,在第2页上,我不断收到未定义变量的通知。你们都看到我看不到的东西吗 ?另外,我只放置相关的HTML。谢谢!

P.S。 -(我读过类似的文章,但他们没有回答我的问题,他们只提到检查赋值运算符。此外,我看了一个示例,上面写着“已经在这里回答”,但对我没有帮助,因为在该stackoverflow文章中,我知道用户名是什么。在我的情况下,这是不同的,因为用户名是用户输入的内容,不是我预先确定的!)

第1页:HTML / PHP

const channel = yield call(createSocketChannel, socket)
const payload = take(channel) //error: not valid channel or         not valid pattern 

第2页:L10_Combo2

<form action="L10_Combo2.php" method="POST">
        <p>Please enter your username</br>
        <input type="text" id="username" name="username"/>        
        </p>
        <p>Please enter your email address</br> 
        <input type="email" id="email" name="email"
        </p>
        </br></br>
        <input type="submit" name="submit" value="Submit Information"/>
        </form>

        <?php
              session_start();

        if(isset($_POST['submit'])){
            $username = $_SESSION['username'];
            $email = $_SESSION['email'];
        }
    ?>    

1 个答案:

答案 0 :(得分:-1)

在注册会话时,它需要转到表单操作页面。看起来您的表单操作在第2页上

<?php
        session_start();
        if(isset($_POST['submit'])){
            $username = $_SESSION['username'];
            $email = $_SESSION['email'];
        }
    ?>    //Move this to page #2

将您的php脚本移至第1页的顶部

<?php
session_start(); //this needs to be on the top most code

if(isset($_POST['submit'])){ //this needs to move to the form action page, looks like its on page #2.
    $username = $_SESSION['username'];
    $email = $_SESSION['email'];
}

?>

您的html位于php代码的顶部之后

<form action="L10_Combo2.php" method="POST">
        <p>Please enter your username</br>
        <input type="text" id="username" name="username"/>        
        </p>
        <p>Please enter your email address</br> 
        <input type="email" id="email" name="email"
        </p>
        </br></br>
        <input type="submit" name="submit" value="Submit Information"/>
        </form>



<?php
session_start(); //this needs to be on the top most code on page 2

if(isset($_SESSION['username']) && isset($_SESSION['email'])){
    $username = $_SESSION['username'];
    $email = $_SESSION['email'];
}

    //This is where I am getting the undefined variable error!
    print("Stored in the session: " . $username .  $email); 
    print_r($_SESSION);

    session_destroy();

也做

 print_r($_SESSION); to see what's in the sessions before session destroy to check what is actually in the session var.