【问题标题】:PHP session variable not being storedPHP会话变量未存储
【发布时间】:2019-12-19 13:39:48
【问题描述】:

我的会话变量没有被存储。

我尝试了很多东西,比如var_dump、print_r,我的变量打印得很好,但没有存储。

这是我的代码:

<?php
session_start();
require "header.php";
?>

    <form action="../Views/index.php?page=createSecteur" method="post">
        <div class="form-group">
            <label for="libelle">Libelle : </label>
            <input type="text" name="libelle" class="form-control" id="libelle" required value="<?php echo isset($_SESSION['libelle']) ? $_SESSION['libelle'] : "" ?>">
        </div>
        <button type="submit" class="btn btn-default" name="insert_secteur" id="insert_secteur">Insert secteur</button>
    </form>

<?php
if (isset($_POST['insert_secteur'])) {
    $_SESSION['libelle'] = $_POST['libelle'];
}
require "footer.php";
?>

更新: 我的 $_POST['libelle'] 给了我一个错误“未定义的索引”

【问题讨论】:

  • 尝试将isset($_POST['insert_secteur'])替换为$_SERVER['REQUEST_METHOD'] == 'POST'
  • 你能稍微解释一下吗?我在那里没有看到任何 var_dump。
  • “未存储”到底是什么意思?没有存储在哪里?您如何尝试“存储”它们?你怎么知道那些没有被“存储”了
  • @LucasMeine 他的意思是他试图通过var_dump跟踪他的数据
  • @Joseph,我明白了,我只是不明白他在哪里检查了他的变量并设置了它们,但没有以某种方式存储在会话中。

标签: php session


【解决方案1】:

尝试检查$_POST['libelle']是否也存在。
还可以尝试通过清空表单的action 来发布到同一页面。
为空时,这将自动成为同一页面。

<?php
session_start();
require "header.php";
?>

    <form action="" method="post">
        <div class="form-group">
            <label for="libelle">Libelle : </label>
            <input type="text" name="libelle" class="form-control" id="libelle" required value="<?php echo isset($_SESSION['libelle']) ? $_SESSION['libelle'] : "" ?>">
        </div>
        <button type="submit" class="btn btn-default" name="insert_secteur" id="insert_secteur">Insert secteur</button>
    </form>

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' AND isset($_POST['insert_secteur']) AND isset($_POST['libelle'])) {
    $_SESSION['libelle'] = $_POST['libelle'];
}
require "footer.php";
?>

【讨论】:

    【解决方案2】:

    如果您删除操作网址(如果您发布到同一页面),它确实有效:

    <?php
        session_start();
        require "header.php";
        ?>
    
            <form action="" method="post">
                <div class="form-group">
                    <label for="libelle">Libelle : </label>
                    <input type="text" name="libelle" class="form-control" id="libelle" required value="<?php echo isset($_SESSION['libelle']) ? $_SESSION['libelle'] : "" ?>">
                </div>
                <button type="submit" class="btn btn-default" name="insert_secteur" id="insert_secteur">Insert secteur</button>
            </form>
    
        <?php
        if (isset($_POST['libelle'])) {
            $_SESSION['libelle'] = $_POST['libelle'];
        }
    
    
        echo $_SESSION['libelle'];
        require "footer.php";
    ?>
    

    如果您发布到其他页面,则必须在其他页面上执行发布。

    但是,如果您需要知道已发送的具体表格,您可以这样做: 通过使用键名保存隐藏的输入

    <?php
        session_start();
        require "header.php";
        ?>
    
            <form action="" method="post">
                <div class="form-group">
                    <label for="libelle">Libelle : </label>
                    <input type="text" name="libelle" class="form-control" id="libelle" required value="<?php echo isset($_SESSION['libelle']) ? $_SESSION['libelle'] : "" ?>">
                    <input type="hidden" name="source" value="createSecteur"/>
                </div>
                <button type="submit" class="btn btn-default" name="insert_secteur" id="insert_secteur">Insert secteur</button>
            </form>
    
        <?php
        if($_SERVER["REQUEST_METHOD"] == "POST"){
            if (empty($_POST["source"])){$source = null;}else{$source = htmlentities($_POST['source'], ENT_QUOTES, "UTF-8");}
    
            if($source=='createSecteur'){
                if (isset($_POST['libelle'])) {
    
                    $_SESSION['libelle'] = $_POST['libelle'];
                }
            }
        }
    
    
        echo $_SESSION['libelle'];
        require "footer.php";
    ?>
    

    【讨论】:

    • 没有任何变化:/
    • 那么你的$_POST['libelle'] 是空的
    • 这可能是因为您可能发布到其他页面?还是页面是一样的?如果页面相同,则将操作设为空,例如 action=""
    • @Q.Rey 使用您的现有代码尝试使用空操作 action=""
    猜你喜欢
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    相关资源
    最近更新 更多