php返回EMPTY回显。如何/为什么?

时间:2017-03-25 14:12:44

标签: php forms

我很难弄清楚为什么我会得到这个空洞的回声。我正在调试,我看到所有值都在正确的位置,但它没有回显!

<body>
<div class='wrapper'>
    <div id='message'>
        <h2>Mail Sent Successfully!</h2>
        <p>We will be in touch soon.</p>
        <?php
            // Echo session variables that were set on previous page
            if (isset($_POST['first_name'])) {
                session_unset();
                session_destroy();
                $_SESSION['name'] = isset ($_POST['first_name']) ? $_POST['first_name'] : "";
                $_SESSION["favcolor"] = "Green ";
                echo "name: " . $_SESSION['name'] . ".<br>";
                echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
            }
        ?>
    </div>

    <form method="POST" id="myform" class="myform" title="Apply Title" action="<?php echo $_SERVER['PHP_SELF']; ?>" style="background-color:#FFFFFF;">
        <fieldset>
            <div>
                <label class="title">First Name</label>
                <input type="text" name="first_name" id='first_name' class="required"  minlength="2" data-msg-required="Please enter your first name" maxlength='128' value="<?PHP if(isset($_POST['first_name'])) echo htmlspecialchars($_POST['first_name']); ?>">
            </div>
            <p id="invalid-first_name" class="error_msg"></p>
        </fieldset>
        <fieldset>
            <div>
                <label class="title">Your employer's company name.</label>
                <input type="text" name="employer" id='employer'>
            </div>
            <p id="invalid-employer" class="error_msg"></p>
        </fieldset>
        <input type="button" name="submit" id="submit_app" class="sub" value="submit"/>
    </form>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src='http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js'></script>
    <script src='http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js'></script>
    <script type="text/javascript" src="formToWizard.js"></script>

    <script type="text/javascript">
        $(document).ready(function(){
        $("#myform").formToWizard({ submitButton: 'submit_app' });
            jQuery.validator.setDefaults({
                errorPlacement: function(error, element) {
                    error.appendTo('#invalid-' + element.attr('name'));
                }
            });
        });
    </script>

在调试代码时,我可以看到刚输入的所有值。但是一旦你提交了表格,它就不会回响!

1 个答案:

答案 0 :(得分:1)

您正在尝试回显不存在的会话。在分配变量之前输入session_start()。

<?php
            // Echo session variables that were set on previous page
            if (isset($_POST['first_name'])) {
                session_unset();

                $_SESSION['name'] = $_POST['first_name'];
                $_SESSION['favcolor'] = "Green";

                echo "name: " . $_SESSION['name'] . ".<br>";
                echo "Favorite color is " . $_SESSION['favcolor'] . ".<br>";
            }
        ?>

编辑:

我清理了你的代码,现在试试。不要破坏会话,你可以根据需要简单地取消它。另外,我不确定你要对$_SESSION['name'] = isset ($_POST['first_name']) ? $_POST['first_name'] : "";做什么,所以我改变了。