繁体   English   中英

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

[英]How to bring data to another page in 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;

class.php:

class myData{
     function __construct($test){
          ***some process***
          $_SESSION["test"] = $test;
     }
}

next.php

$next = $_SESSION["test"];

您可以将变量存储在类中,然后使用getter方法获取该数据。 file1.php

if(isset($_POST['submit'])){
     $data = new MyData($_POST['test']);
}


class MyData{
     private $data = '';

     function __construct($test){
          ***some process***
          $data = $test;
     }
     function getData() {
         return $this->data;
     }
}

file2.php

    include file1.php;
    echo $data->getData();

我声明一个函数,然后返回next.php第1页

function showContent($content){
      if(!empty($content)){
            return $content;
      }
      return false;
}

然后在class.php ,我声明一个吸气剂为Karim Aljandali先生的答案。 在页面1上,添加一行$next = showContent($data->getData());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM