PHP 会话在返回第一页时无法处理多页表单

PHP sessions not working on multiple page form when going back to the first page

当我尝试在第一页上回显结果时,PHP $_SESSION 无法正常工作。错误消息基本上指出变量未定义。

我要使用的路径:

  1. 第 1 页 = 表格 1
  2. 第 2 页 = 表格 2
  3. 返回第 1 页 = 表单 1,其中填写了上次提交的所有输入 + 文本中 2 个表单的所有数据。

这可能吗?

第 1 页 = index.php:

    <?php session_start(); ?>
    <!DOCTYPE html>
    <html lang="fr">
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    </head>
    
    <body>   
        <form action="overview.php" id="regForm" method="post">
            
            <div class="tab">
                <h2>1. Your info</h2>
                <p><input type="text" placeholder="Prénom"  name="fname" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
                <p><input type="text" placeholder="Nom" name="name" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
                <input type="submit" value="Valider">
            </div>
        </form>
    <?php echo "Your name is :",$_SESSION['name'], "and your first-name is ", $_SESSION['fname'];?>
<?php echo "Your e-mail is :", $_SESSION['email'] ;?>
    </body>
    </html>

第 2 页 = overview.php:

<?php session_start(); ?>

<?php

   $_SESSION['fname'] = $_POST['fname'];
   $_SESSION['name'] = $_POST['name'];

?>

<form action="index.php" id="regForm" method="post">   
        <h2>1. Tes informations personnelles</h2>
        <p><input type="text" placeholder="e-mail" name="email"></p>
        <input type="submit" value="Valider">
</form>

返回第1页=index.php:

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
</head>

<body>   
    <form action="overview.php" id="regForm" method="post">
        
        <div class="tab">
            <h2>1. Your info</h2>
            <p><input type="text" placeholder="Prénom"  name="fname" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
            <p><input type="text" placeholder="Nom" name="name" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
            <input type="submit" value="Valider">
        </div>
    </form>

<?php echo "Your name is :",$_SESSION['name'], "and your first-name is ", $_SESSION['fname'];?>
<?php echo "Your e-mail is :", $_SESSION['email'] ;?>

</body>
</html>

你们有没有看到任何阻止代码 运行 的问题?

刚刚找到“原因”。 当我启动项目时,我基本上阻止了 cookie,这阻止了 $_sessions 正常工作。 因此,请确保您的网站上启用了 cookie。那种浪费一个小时工作的事情...