php 返回一个空的回声.如何/为什么



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

<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>

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

您正在尝试回显不存在的会话。在分配变量之前放置 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'] : "";做什么,所以我改变了它。

最新更新