1

Is it possible to pass an Object through a Hidden Field in an HTML Form using $_POST and retrieve that Object on the page that the form links to?

On the first page, I have a form like the one below:

<?php
session_start();
require_once '../Model/player.php'; // To Enable Creation of a New Player Object

$playerName = filter_input(INPUT_POST, 'playerName');
$playerNumber = 1;

$player = new player($playerName, $playerNumber);

if (isset($player))
{
  echo '<p>Successfully created your player!</p><br>';
?>

<form class="viewStats" action="../View/displayPlayerStatsView.php" method="post">
  <input type="hidden" name="playerObject" value="<?php echo $player; ?>">
  <input type="submit" value="View Your Player's Stats">
</form>

<?php
  }
?>

And on the second (receiving) page, I have code like the code below:

session_start();
require_once '../Model/player.php'; // To Use Player Object

$player = filter_input(INPUT_POST, 'playerObject'); // ERROR: Thinks the Player Object is a string.

My error seems to be that the receiving page that retrieves the 'playerObject' from the $_POST array is acting like the Object is a string.

Can anyone give me guidance on how to pass an Object from one page to another using the $_POST array? Is this even possible? Thank you in advance.

UPDATE: Based on suggestions to serialize the Object, I now am getting the following errors:

If I change my code on the first (sending) page to:

$playerSerial = serialize((object) $player);
<form class="viewStats" action="../View/displayPlayerStatsView.php" method="post">
      <input type="hidden" name="playerObject" value="<?php echo $playerSerial; ?>">
      <input type="submit" value="View Your Player's Stats">
</form>

and change the code on the second (receiving) page to:

$playerSerial = filter_input(INPUT_POST, 'playerObject');
print_r($playerSerial);
$player = unserialize($playerSerial);

then the output I get from print_r($playerSerial); is O:6:, which I know is incorrect since the object has properties holding a player's name, number, health, strength, etc.

The require_once '../Model/player.php'; code exists in both PHP files, and it comes right at the top of both before any other code is executed.

TechAust10
  • 67
  • 3
  • 11
  • 4
    It's funny, but everything you get through POST is a string, something to do with the way http works. You'd have to serialise it, and unserialize it at the receiving end; but surely better to simply use session for something like this – Mark Baker Apr 21 '16 at 20:51
  • 2
    if it's sent over the web embedded in a request, it's a string. That's the way http works. You can always serialize objects, on code images, etc. and send them that way, but that's basically just converting them to a string in a way that you can convert them back after. Short answer: no. Long answer: whatever attributes you want to send, send those values as strings, and on the other end new up another object that has the same values – alexanderbird Apr 21 '16 at 20:51
  • For the record, I typed that before seeing @MarkBaker's comment - funny how similar the first lines are – alexanderbird Apr 21 '16 at 20:54

2 Answers2

1

You have to make a few additions and corrections:

<?php 

//... your previous code

$player = serialize($player);
?>

<form class="viewStats" action="../View/displayPlayerStatsView.php" method="post">
  <input type="hidden" name="playerObject" value="<?php echo $player; ?>">
  <input type="submit" value="View Your Player's Stats">
</form>

Use the serialize() function to create a string that can be passed to your other page which you can unserialize() as follows:

secondPage.php:

$player = $_POST['playerObject'];
$player = unserialize($player);

Also, you forgot to use echo here:

change

value="<?php $player ?>"

to

value="<?php echo $player; ?>"
Webeng
  • 7,050
  • 4
  • 31
  • 59
  • I made the changes exactly as suggested. I have another error: the serialization/unserialization is not working. I get errors from PHP saying: Notice: unserialize(): Error at offset 0 of 4 bytes in C:\xampp\htdocs\BattleRoyale\View\displayPlayerStatsView.php on line 12 Call Stack # Time Memory Function Location 1 0.0006 135672 {main}( ) ..\displayPlayerStatsView.php:0 2 0.0013 163088 unserialize ( ) ..\displayPlayerStatsView.php:12 ( ! ) Fatal error: Call to a member function displayPlayerStats() on boolean in C:\xampp\htdocs\BattleRoyale\View\displayPlayerStatsView.php on line 26 – TechAust10 Apr 22 '16 at 00:34
  • If I perform `$playerSerial = filter_input(INPUT_POST, 'playerObject'); print_r($playerSerial); $player = unserialize($playerSerial);` the output I get from `print_r($playerSerial);` is **O:6:**, which I know is incorrect since the object has properties holding a player's name, number, health, strength, _etc._ – TechAust10 Apr 22 '16 at 00:36
  • @TechAus10 do you have `require_once '../Model/player.php'; // To Enable Creation of a New Player Object` in the other file as well? – Webeng Apr 22 '16 at 00:40
  • Yes, the `require_once '../Model/player.php';` code exists in both PHP files, and it comes right at the top of both before any other code is executed. – TechAust10 Apr 22 '16 at 00:43
1

Get yourself into serialization: the process of making a string from the object, which can in future be deserialized from the string back to object.

Some docs, which could be useful for you:

PHP - How object serialize/unserialize works?

http://php.net/manual/ru/oop4.serialization.php

http://www.phpinternalsbook.com/classes_objects/serialization.html

Community
  • 1
  • 1
Bandydan
  • 623
  • 1
  • 8
  • 24