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 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真