Reputation: 6685
I would like to pass 2 php objects as data, as they are, to $.post.
$(function() {
$("button").click(function() {
$.post("fight.php",{player: "<?php $player;?>", enemy: "<?php $enemy; ?>"},function(result) {
$("#displayFight").html(result);
});
});
});
$player
, $enemy
are 2 different objects with a few properties each. I would like to pass them as the whole objects they are, so that fight.php
can deal with them.
I've also tried serialize
, but to no avail:
{player: $("<?php $player ?>").serialize()}
How can I do this?
Note: With all the methods I've tried, I either get some 'unknown identifier' or $_POST comes out empty on the receiving page.
EDIT: the json_encode part works, but the decoding fails. On the receiving page:
$player = json_decode($_POST["player"])
fails and returns an error: json_decode() expects parameter 1 to be string, array given
Upvotes: 3
Views: 6667
Reputation: 28753
One suggestion could be to JSON encode the PHP object using json_encode
.
However, let's think about this: you have a PHP script generating output. That output - which contains this Javascript snippet and the 2 objects - is sent to the client-side (the browser) where it is executed, and it re-sends those two objects back to the server with an AJAX call.
So an alternative idea might be to simply store the objects server-side the whole time in the session and only send an identifier instead of the entire object:
<?php
// apologies for errors - my PHP's rusty
$_SESSION[$player->uuid()] = $player;
$_SESSION[$enemy->uuid()] = $enemy;
?>
$.post("fight.php",{player: "<?php $player->uuid();?>", enemy: "<?php $enemy->uuid(); ?>"},function(result) {
// ...
});
Then the fight.php
script would restore then:
// is this still how PHP handles sessions?
$player = $_SESSION[$_POST['player']];
$enemy = $_SESSION[$_POST['enemy']];
If you're using a conventional framework to build a database driven application, this works particularly well because chances are your objects are stored in a database and can be read out of the database easily - which may be a better solution than using the $_SESSION
array:
// if recollection serves this is CakePHP-ish
$player = $this->Player->find($_POST['player']);
$enemy = $this->Enemy->find($_POST['enemy']);
Upvotes: 1
Reputation: 2166
Try using json_encode
http://www.php.net/manual/en/function.json-encode.php
{player: <?php json_encode($player); ?>}
Upvotes: 1
Reputation: 437444
You need to use json_encode
, like this:
$.post("fight.php",{
player: <?php echo json_encode($player);?>,
enemy: <?php echo json_encode($enemy);?>
}, ... );
Note that there are no quotes around the <?php ?>
tags; json_encode
will add them automatically if required.
Apart from that, you need to be aware of the requirements that json_encode
imposes: most importantly, if your objects contain string content (either as a property or as a property of any sub-objects) then these strings must be encoded in UTF-8.
Upvotes: 3
Reputation: 2964
To easily communicate between PHP and Javascript you should properly serialize your objects. The easiest way is to call json_encode($player) in your PHP code. Then when you are fetching it again with PHP you need to call json_decode() on it and properly initialize the object. There's no way for PHP and Javascript to share an instance of the object. Only the serialized data.
Upvotes: 1