Alexis
Alexis

Reputation: 37

How to bring data to another page in php

As the question, I can do that with $_SESSION on the below code, but I would like to do that without using $_SESSION, any suggestion ? I am new for using oop in php.

PHP (page 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"];

Upvotes: 0

Views: 262

Answers (2)

Alexis
Alexis

Reputation: 37

I declare a function and return back to page 1 on next.php

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

Then on the class.php, I declare a getter as Mr.Karim Aljandali's answer. On the page 1 I add a line $next = showContent($data->getData());

Upvotes: 0

Karim Aljandali
Karim Aljandali

Reputation: 136

You could store the variable in the class and then use a getter method to get that data back. 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();

Upvotes: 1

Related Questions