【问题标题】:What am I doing wrong with this phone SESSION I'm trying to print to screen?我正在尝试打印到屏幕上的这款手机 SESSION 我做错了什么?
【发布时间】:2019-06-09 04:58:57
【问题描述】:

我正在测试一个会话,其中前三页打印到最后一页。但我很难让它工作。一切顺利,直到最后一页。代码如下:

<?php
session_start();

if (isset($_POST['submit'])) { 
$_SESSION['name'] = $_POST ['name'];
$_SESSION['email'] = $_POST ['email'];
$_SESSION['age'] = $_POST ['age'];

echo $_SESSION['name'];
echo $_SESSION['email'];
echo $_SESSION['age'];
}
?>

会话变量出现空白或索引错误。任何人都可以帮助打印用户输入的会话数据的正确方法吗?

这些是第 1、2 和 3 页:

<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method="POST" action="test_2_page_1.php">
<input type="text" name="name" placeholder="enter name"></input>
<input type="submit"> next</input>
</form>
<?php
//Set session variables
if (isset($_POST['submit'])) { 
$_SESSION['name'] = $_POST ['name'];
}
?>
</body>
</html>


<?php
session_start();
if (isset($_POST['submit'])) { 
$_SESSION['name'] = $_POST ['name'];
$_SESSION['email'] = $_POST ['email'];
}
?>
<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method="POST" action="test_2_page_2.php">
<input type="text" name="email" placeholder="enter email"></input>
<input type="submit"> next</input>
</form>
<?php
// Set session variables
?>
</body>
</html>


<?php
session_start();
if (isset($_POST['submit'])) { 
$_SESSION['name'] = $_POST ['name'];
$_SESSION['email'] = $_POST ['email'];
$_SESSION['age'] = $_POST ['age'];
}
?>
<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method="POST" action="post_test.php">
<input type="text" name="age" placeholder="enter age"></input>
<input type="submit"> next</input>
</form>
<?php
// Set session variables
?>
</body>
</html>

【问题讨论】:

  • 您能分享一下您发布的表单是什么样的吗?
  • 我刚刚更新了原帖。
  • 您有一些无效的 HTML 标签。您永远不会进入test_2_page_2.php 中的条件,您需要将if (isset($_POST['submit'])) { 替换为if (!empty($_POST)){

标签: php session post session-variables


【解决方案1】:

您的 HTML 中没有名称为 submit 的输入元素,因此您的 PHP 代码的以下部分将始终计算为 false

if (isset($_POST['submit'])) {

将 HTML 中的提交按钮更改为以下内容,当表单发布时,上面的行将评估为 true

&lt;input name="submit" type="submit"&gt; next&lt;/input&gt;

【讨论】:

  • 请像我五岁一样解释
  • PS 没用。我最终遇到了更多错误。
【解决方案2】:

input 元素是 self-closing,因为您不需要 &lt;/input&gt; - 而是使用 /&gt; 简单地关闭标签

要将 Next 显示为提交按钮,您可以使用 value 属性 - 即:&lt;input type='submit' value='Next' /&gt;

分配会话变量后,您无需继续尝试设置它,如果该变量在 POST 数组中不可用,则会遇到错误 - 因此在创建变量之前使用 isset 进行测试

<?php
    session_start();
    if ( isset( $_POST['submit'], $_POST['name'] ) ) { 
        $_SESSION['name'] = $_POST['name'];
    }   
?>
<html>
    <head>
        <title>phpsession</title>
    </head>
    <body>
        <form method="POST" action="test_2_page_1.php">
            <input type="text" name="name" placeholder="enter name" />
            <input type="submit" name='submit' value='Next'>
        </form>
    </body>
</html>





<?php
    session_start();
    if ( isset( $_POST['submit'], $_POST['email'] ) ) {
        $_SESSION['email'] = $_POST['email'];
    }
?>
<html>
    <head>
        <title>phpsession</title>
    </head>
    <body>
        <form method="POST" action="test_2_page_2.php">
            <input type="text" name="email" placeholder="enter email" />
            <input type="submit" name='submit' value='Next'>
        </form>
    </body>
</html>







<?php
    session_start();
    if( isset( $_POST['submit'],$_POST['age'] ) ) {
        $_SESSION['age'] = $_POST['age'];
    }
?>
<html>
    <head>
        <title>phpsession</title>
    </head>
    <body>
        <form method="POST" action="post_test.php">
            <input type="text" name="age" placeholder="enter age" />
            <input type="submit" name='submit' value='Next'>
        </form>
    </body>
</html>

为了进一步帮助您解决问题,我快速整理了一个单页演示,用于根据表单提交获取和设置会话变量。希望对您有所帮助

<?php
    session_start();
    #https://stackoverflow.com/questions/54194496/what-am-i-doing-wrong-with-this-phone-session-im-trying-to-print-to-screen/54194890?noredirect=1#comment95217192_54194890

    if( !empty( $_GET['reset'] ) ){
        unset( $_SESSION['name'] );
        unset( $_SESSION['email'] );
        unset( $_SESSION['age'] );
        header( sprintf('Location: %s', $_SERVER['SCRIPT_NAME'] ) );
    }

    /* 
        as each form only submits two values ( submit being one ) we can
        simply remove it from the POST array and use the remaining field/value
        to generate the session variables using simple array techniques

        The below could easily be replaced with pre-defined variables, such as:
        if( isset( $_POST['name'] ) )$_SESSION['name']=$_POST['name']; etc

    */
    if ( isset( $_POST['submit'] ) ) {
        /* remove the submit button & value from the array */
        unset( $_POST['submit'] );

        /* there will now be one item, with index = 0 */
        $keys=array_keys( $_POST );
        $values=array_values( $_POST );

        /* set the session variable */
        $_SESSION[ $keys[0] ]=$values[0];
    }

?>
<html>
    <head>
        <title>phpsession</title>
    </head>
    <body>
        <form method='POST'>
        <?php
            if( empty( $_SESSION['name'] ) ){
                echo "<input type='text' name='name' placeholder='enter name' />";
            }
            if( !empty( $_SESSION['name'] ) && empty( $_SESSION['email'] ) ){
                echo "<input type='text' name='email' placeholder='enter email address' />";
            }
            if( !empty( $_SESSION['name'] ) && !empty( $_SESSION['email'] ) && empty( $_SESSION['age'] ) ){
                echo "<input type='text' name='age' placeholder='enter your age' />";
            }
            if( empty( $_SESSION['age'] ) ){
                echo "<input type='submit' name='submit' value='Next'>";
            } else {
                if( empty( $_POST['finish'] ) ) echo "<input type='submit' name='finish' value='Finish'>";
            }
            if( isset( $_POST['finish'] ) ){
                /* 
                    Once the user has completed each form, send them off to their final destination?
                    or do something else....
                */
                printf(
                    'Do some interesting stuff now ....
                    <pre>%s</pre><a href="?reset=true">Reset</a>',
                    print_r( $_SESSION, true )
                );
            }
        ?>
        </form>
    </body>
</html>

【讨论】:

  • 什么不起作用?上面的 html sn-ps 中的表单动作是同一个页面吗?
  • 不,它跨越 3 页。最后应该发布但没有。在 est 时间感到困倦。整晚都在努力让这件事发挥作用。如果我不回复,我会在早上第一件事。
  • 我运行了上面的代码,直到最后一页我仍然没有任何或未定义的变量错误。
  • ?真的吗 ?我刚刚重新运行了上面的,我没有收到任何错误,请告诉我消息是什么
  • 注意:未定义索引:
猜你喜欢
  • 2022-11-17
  • 2022-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 2020-08-30
相关资源
最近更新 更多