View Full Version : Getting Started with Ajax

01-26-2007, 04:46 PM
I just completed putting up a pretty simple game site that uses php for everything (almost everything). The game is opensource and written by others. I consider myself at rookie at php, and was capable of modifying the game a little bit with some of great help from all of the members here.

The game constantly sends queries to the database, and then will refresh the screen.

I know this is a pretty open ended question, and I just bought a book to begin reading this weekend. But I am curious to peoples thoughts on the difficulty level in incorporating ajax into existing php scripts? Something a php rookie should be able to figure out?

01-26-2007, 06:20 PM
Just make sure you understand how PHP works with forms and browser http requests and you'll be able to incorporate Ajax once you understand how Ajax works.

02-03-2007, 06:03 PM
Thanks for the reply.

Well, I have a book, and I'm pretty much lost at the moment, but hoping maybe somebody can give a small example of the following. I'm also experimenting with some scripts from zapatec.com (http://www.zapatec.com/website/main/)

I have a basic form that says something like:

"You have 20 armies available to place" The '20' is being read from the database. You simply indicate the number you want to place and press submit.

I'm trying to have the form submit, and then for the form to update itself, indicating the number of armies you have left. So, if you placed 5, the form will change to say you have 15.

If it helps, I've included the form below. I think/hope if I can understand how to do that, then I may be able to convert everything else eventually.

<form action="includes/func_addarms.php" method="post" name="statusaction" id="addarmy">
<br />
<? // Get the number of armies the player has available to place
$sql = 'SELECT armies FROM game_'.$_SESSION['game_id'].' WHERE id = '.$_SESSION['player_id'];
$numarmies = get_one($sql);

<input class="button" type="hidden" name="numarmies" value="<?= $numarmies ?>">
<input class="button" type="hidden" name="gamestate" value="<?= $gamestate ?>">
You have <?= $numarmies ?> armies to place.
<br /><br />

Armies: <input class="button" name="armies" type="text" size="1">
<select class="button" name="fromstate">
<option selected value="invalid">Select a Country:</option>
<? // Show only the states the current player controls.
while($a_state = current($_SESSION['STATES'])){
if($a_state['player'] == $_SESSION['player_id']){ ?>
<option value="<?= key($_SESSION['STATES']) ?>"><?= $a_state['name'] ?></option>
<? }
} reset($_SESSION['STATES']); ?>
<br /><br/>
<input class="button" type="submit" value="Place Armies">