<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The Ultimate War Game</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="_assets//js/ajax.js" type="text/javascript"></script>
</head>
<body>
<?php
include("/_assets/inc/war.php");
include("/_assets/inc/player.php");
$war = new War();
$deck_one = $war->getPlayerOneDeck();
$deck_two = $war->getPlayerTwoDeck();
$player_one = new Player($deck_one);
$player_two = new Player($deck_two);
?>
<a id="next-card" href="javascript:void(0)">Next Play</a>
<div class="table">
<div class="hands">
</div>
</div>
</body>
</html>
I have two classes (code not required). From the code provided I create a war class, player one class, and player two. In a nutshell what this does is deals cards to two players and creates the class.
My problem is I am trying to create an ajax function below called Next Play. When clicked I can open up another PHP file but i cannot access $war, $player_one, or $player_two.
The other issue is if I put the PHP code in the ajax call then every time I click “Next Play” it recreates these objects.
The ultimate goal is I am trying to create these objects then access the properties of these through ajax or any other suggested method without recreating the objects. I hope this is not confusing.
With my assumtion Chad, your data stuctures are NOT permanent through your play.
You said you put your code in an ajax calls.
Try to think of the nature of an ajax call, you call an ajax function (let say ‘next play’),
then the function calls some page outside it.
The mechanism is just like you browse the web in an ordinary way,
it request for some objects every time you make a call, so it’s not so persistent with your current data structure.
If you want to store playing status, just try to store data somewhere quite persistent,
like in cookie or a DB. Or you can move some mechanisms/algorithms into JavScript instead,
like keeping war or deck status in JS variables. Even adding a callback function to keep your status after an ajax call would help.
This way, you don’t have to store your data in a DB or cookie.
Hope this help. 🙂