HTML POST 到多个 .php 文件

这是我目前的形式。



## on index.php

        <form action="index2.php" method="POST">

            <select name="profile">

                <option value="basic" selected>Easy</option>

                <option value="intermediate">Medium</option>

                <option value="advanced">Hard</option>

            </select>

            <br>

                <input type="submit"/>

        </form>

在index2.php上,我可以成功执行$_POST['profile']。但是,我想将 $_POST['profile'] 放在我拥有的另一个 .php 文件上,该文件名为 test.php。


当我尝试在 test.php 上执行 $_POST['profile'] 并将其分配给变量 $test 时,会返回错误。


Notice: Undefined index: profile in C:\xampp\htdocs\testfolder\test.php on line 15

导致错误的代码:


$test = $_POST['profile'];


## this is on test.php

预期结果:成功发布“个人资料”。


我怎样才能这样做呢?


慕沐林林
浏览 68回答 1
1回答

炎炎设计

您可以在 POST 之后将该变量存储到会话变量中,并且可以从每个页面的代码中的任何位置访问该变量。您的表格:<form action="index2.php" method="POST">    <select name="profile">        <option value="basic" selected>Easy</option>        <option value="intermediate">Medium</option>        <option value="advanced">Hard</option>    </select>    <br>        <input type="submit"/></form>在页面index2.php上,您必须使用此index2.php捕获POST数据<?php session_start(); // starting the sessionif(isset($_POST)){      $_SESSION['profile'] = $_POST['profile'] // sets the $_POST['profile'] value to $_SESSION['profile'] }?>现在 $_SESSION 将成为您的全局变量,您可以在任何页面随时访问它,只需添加 session_start(); 在每一页上。测试.php:<?php session_start(); //starting the sessionecho $_SESSION['profile'];?>它会在任何页面上返回您想要的值。
打开App,查看更多内容
随时随地看视频慕课网APP