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.

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

报告相同问题?