dptj13337 2014-10-27 16:55
浏览 58
已采纳

无法将会话数据存储到下一个表单中

Currently I am on a question to store session and pass it to the next form, but as hard as I try to start session and store the variable inside the session, it does not work. Please shed some light on this, thanks!

MainForm.php

<?php
session_start();
require("inputValidation.php"); // This php file is just a file that does validation for my side
$validForm = true;
if ($_POST) 
{
}
?>
<form class="form" action="nextform.php" role="form" method="post">
<input type="text" class="form-control" id="name" name="name" value= "<?php
                        if (isset($_POST['name'])) 
                            {
                            if (!validateRequired($_POST['name'])) 
                            {
                                $_SESSION['test'] = $_POST['name'];
                                $validForm = false;          
                            }
                            if (validateRequired($_POST['name'])) 
                            {
                                $_SESSION['test'] = $_POST['name'];
                                $validForm = true;
                            }
                        }
                        ?>
<input name="submit" type="submit" value="Submit" class="btn btn-primary">

nextform.php

print_r ($_SESSION);

The problem is even if i enter any value inside the textbox name, I will be just redirected straight to nextform.php without getting my session value. Why is this so? Is there any way I can get my session value without changing action="nextform.php"?

Thank you!

Sorry I am still new to sessions and PHP so bear with me :)

  • 写回答

1条回答 默认 最新

  • douzaoqu4447 2014-10-27 17:02
    关注

    Basically what you wan't to do is move the whole

    if (isset($_POST['name']))
    {
        //... you code
    }
    

    to the page where the form is submitted, in your case nextform.php.

    Your pages should be something like:

    MainForm.php

    <form class="form" action="nextform.php" role="form" method="post">
    <input type="text" class="form-control" id="name" name="name" value= "">
    <input name="submit" type="submit" value="Submit" class="btn btn-primary">
    

    nextform.php

    <?php
    session_start();
    require("inputValidation.php"); // This php file is just a file that does validation for my side
    $validForm = true;
    
    if (isset($_POST['name'])) 
    {
        if (!validateRequired($_POST['name'])) 
        {
            $_SESSION['test'] = $_POST['name'];
            $validForm = false;          
        }
        if (validateRequired($_POST['name'])) 
        {
            $_SESSION['test'] = $_POST['name'];
            $validForm = true;
        }
    }
    
    if($validForm) {
        echo "Validation success";
        print_r ($_SESSION);
    } else {
        echo "Form validation failed.";
    }
    ?>
    

    Basically you can access the POST parameters in the page where the form is submitted, see action="nextform.php".

    Here you can check if the form was submitted and do the proper validation, along with redirecting the user somewhere, or just showing the appropriate messages as needed, ie Validation failed or Validation success.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭