如何在php中将数据带到另一个页面

作为问题,我可以$_SESSION在下面的代码中做到这一点,但我想在不使用的情况下做到这一点$_SESSION,有什么建议吗?我是在 php 中使用 oop 的新手。


PHP(第 1 页):


    include './class.php';

    include './next.php';


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

         $data = new myData();

         $data->setTest($_POST['test']);

    }


    $content = '<form method="post" action="next.php">

      <input type="text" name="test" id="test" value="test"/>

      <input type="submit" name="submit" value="Submit"/>

    </form>';


   if(!empty($next)){

       $content = $next;

   }


   echo $content;

类.php:


class myData{

     function __construct($test){

          ***some process***

          $_SESSION["test"] = $test;

     }

}

下一个.php


$next = $_SESSION["test"];


德玛西亚99
浏览 291回答 2
2回答

qq_遁去的一_1

您可以将变量存储在类中,然后使用 getter 方法取回该数据。文件1.phpif(isset($_POST['submit'])){&nbsp; &nbsp; &nbsp;$data = new MyData($_POST['test']);}class MyData{&nbsp; &nbsp; &nbsp;private $data = '';&nbsp; &nbsp; &nbsp;function __construct($test){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ***some process***&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data = $test;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;function getData() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return $this->data;&nbsp; &nbsp; &nbsp;}}文件2.php&nbsp; &nbsp; include file1.php;&nbsp; &nbsp; echo $data->getData();

翻阅古今

我声明了一个函数并返回到第 1 页next.phpfunction showContent($content){&nbsp; &nbsp; &nbsp; if(!empty($content)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $content;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return false;}然后在 上class.php,我声明一个吸气剂作为Karim Aljandali先生的答案。在第 1 页上,我添加了一行$next = showContent($data->getData());
打开App,查看更多内容
随时随地看视频慕课网APP