为了账号安全,请及时绑定邮箱和手机立即绑定

PHP会话未显示值

PHP会话未显示值

PHP
大话西游666 2022-10-22 15:57:18
<?php    session_start();?><html>    <head>        <link href="style.css" rel="stylesheet" type="text/css">    </head>    <body>        <div class="pageContainer">            <form action="second.php" class="formLayout" method="post">                <div class="formGroup">                    <label>Name:</label>                    <input type="text" name="first_name"><br>                    <input type="hidden" name="postback" value="true"><br>                </div>                <div class="formGroup">                    <label> Car model:</label>                    <div class="formElements">                        <input type="radio" name="model" value="Mustang">Ford Mustang<br>                        <input type="radio" name="model" value="Subaru">Subaru WRX STI<br>                        <input type="radio" name="model" value="Corvette">Corvette<br>                    </div>                    <input type="submit" name="submit">            </form>            <?php                if (isset($_POST['submit'])) {                    $_SESSION["first_name"] = $_POST["first_name"];                    $_SESSION["model"] = $_POST["model"];                }            ?>        </div>    </body></html>我设置我的代码以将“first_name”和“model”名称的值设置到我的会话变量中。当我尝试访问表单提交页面中存储变量的值时:<?php    session_start();?><html>    <body>        <?php            echo $_SESSION["first_name"];            echo $_SESSION["model"];        ?>    </body></html>我只收到模型值之外的值,而不是 first_name 值。我不明白我在这里做错了什么。
查看完整描述

1 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

假设您的第一个文件名为first.php. 如您所示<form>,第二个是second.php.

似乎您正在将数据写入first.php文件中的会话,但是此代码将永远不会运行,因为您将表单提交给second.php而不是first.php

因此,请移动写入会话变量的代码second.php。要测试它是否真的有效,您可以创建一个third.php来显示它们。

(我不知道为什么要看到这个model集合,但我猜你之前的测试中它仍然存在。)


查看完整回答
反对 回复 2022-10-22
  • 1 回答
  • 0 关注
  • 110 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号