Variable storage and updating fields with AJAX

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

48 Replies - 2804 Views - Last Post: 14 February 2013 - 07:38 AM

#1 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 31-August 12

Variable storage and updating fields with AJAX

Posted 08 February 2013 - 12:49 PM

I'm working on a learning project of making a browser based game.

So far the PHP side of the game is going quite well. I have a solid base for my registration/login system that successfully sends an email to the player for activation and inserts the players data into my database tables.

Now I find myself at a bit of a crossroads because I really want this game to use AJAX to update the display. So when something happens like gaining resources or getting attacked happens, the appropriate things will update without having to reload the page.

The problem I'm having are these:

First, I don't know AJAX yet. I'm reading guides and watching tutorials but none of them seem to explain it in a way that allows me to get my head around it.

Second, I'm trying to figure out the smart way to store the player data that is needed in each of the pages. The solutions I came up with were 1. store the values in the session at login. 2. Store the variables in a separate script and require that script on the needed pages. 3. Make database calls on each and every page that requires the data.

The first thing I tried was the script that would be required by each of the pages that had all the database entries needed already setup in variables so that all I would have to do is call them from that script.

No luck so far. It seems that whenever I call them the variable doesn't have string data in it anymore. No idea why.

What I really need right now is someone to help get me on the right track.

Please let me clarify, I am in no way asking for someone to do my work for me. I wouldn't want that, this is my project and I want to do the work. But I do need someones help getting out of the rut that I find myself in.

So basically I need some advice on what to do about updating each pages fields with the database information, how I should store it and whatnot. And some help getting my head around updating the fields with AJAX.

I don't want to get bogged down and loss hope in this rut because I found that as I wrote the login and registration script in PHP, things became a lot clearer and I started to grasp a lot of things that I didn't before. So I'm hoping to keep my momentum up. :)

Thanks guys! I look forward to learning from you!

NOTE: I didn't include any scripts because I didn't know exactly where this would go so I figured I would get some input and then post the relevant scripts as needed.

Is This A Good Question/Topic? 0
  • +

Replies To: Variable storage and updating fields with AJAX

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8849
  • View blog
  • Posts: 33,158
  • Joined: 12-June 08

Re: Variable storage and updating fields with AJAX

Posted 08 February 2013 - 12:58 PM

What does 'ajax' stand for? "Asynchronous Javascript and XML". It's job is to shuffle data from places. The nameless truck driver hauling cargo. If you want to manipulate divs or objects on a webpage that's javascript and the 'DOM' (Document Object Model).

With that bit cleared up it should be something like:
user clicks on the screen
javascript event kicks off and needs to shove data into the db.
the javascript calls an ajax call that sends the data to a php page (without needing to load the php page)
the php page does what ever
the ajax catches the return
and the javascript uses the DOM to update anything on the screen.

Coincidentally I was helping another user with this problem this morning.
http://www.dreaminco...n-ajax-and-php/


If you want a book I have reviewed one recently on Javascript and Jquery (and it has ajax in there as well).
Was This Post Helpful? 1
  • +
  • -

#3 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 31-August 12

Re: Variable storage and updating fields with AJAX

Posted 08 February 2013 - 01:16 PM

I'd be happy for a book on the subject, I appreciate that.

My question would be, is the book going to address the things that I need to have done for my project?

And also, while I am more than happy to read about it, I tend to be an interactive learner. I learn best by discussing things with another human being and putting what is learned into practical use, if someone is willing. :)
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8849
  • View blog
  • Posts: 33,158
  • Joined: 12-June 08

Re: Variable storage and updating fields with AJAX

Posted 08 February 2013 - 01:26 PM

Quote

My question would be, is the book going to address the things that I need to have done for my project?

Yes? No? Honestly I am a bit lost on what you wanted to do (being you were misapplying terms), but the book will take you from little to no javascript/jquery knowledge and put you into the 'usable' and 'moderate' level. Here's the review..

http://www.dreaminco...ipt-and-jquery/

Then when you are done reading the review go to the amazon page and read the index.

Then make the conscious decision of combining what you have read and seen versus your needs.


Quote

And also, while I am more than happy to read about it, I tend to be an interactive learner. I learn best by discussing things with another human being and putting what is learned into practical use, if someone is willing. :)

That's all fine and dandy, but it doesn't sound like you have another human being there to mentor you each step along what ever path you have devised. It may be best to not shut down other avenues of learning.
Was This Post Helpful? 0
  • +
  • -

#5 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 423
  • View blog
  • Posts: 1,801
  • Joined: 30-April 10

Re: Variable storage and updating fields with AJAX

Posted 08 February 2013 - 01:30 PM

My advise is to study Javascript first. get a good understanding of it and then begin to learn ajax and look into the jquery library.

Jquery would be great for a browser based game because it has alot of shortcuts. Do learn Javascript first though as it is written in Javascript.
Was This Post Helpful? 0
  • +
  • -

#6 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 31-August 12

Re: Variable storage and updating fields with AJAX

Posted 08 February 2013 - 01:41 PM

Oh don't misunderstand what I mean. I am in no way shutting down anything.

I'm happy to read on the subject.
Was This Post Helpful? 0
  • +
  • -

#7 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 31-August 12

Re: Variable storage and updating fields with AJAX

Posted 09 February 2013 - 10:33 PM

Ok, after much searching I was able to get a hold of a copy of that book modi linked.

Just FYI, it's every bit as good as Modi says it is in his review. I breezed through the Javascript portion like nothing. Though I did learn quite a bit.

I found the Jquery section extremely enlightening and I'm still reading through it and getting my head around a few things.

But after I got the hang of the basic Jquery syntax and such I decided to skip ahead to the AJAX section and then to see if I could make practical application of these things on my project.

I feel like I'm closer to understanding what I need to do but it's still just out of my reach.

Here is a small portion of my scripts that I've changed since reading the above mentioned book.

First up is the PHP script that has a Mysql query to my player database.
resources.inc.php
<$php
require "connection.inc.php";
$user_id = $_SESSION['user_id'];

$resource_query = "SELECT `empire_name`, `ruler_name`, `population`, `currency`, `scrap`, `food`, `farms`, `housing`,  `barracks`, `militia`, `infantry`
                  FROM `player_data`
                  WHERE `id`='$user_id'";

$resource_result = mysqli_query($resource_query);
$resource_rows = mysqli_fetch_assoc($resource_result);

echo json_encode($resource_rows);
?>



Next up is the beginning of my js file for receiving the data. Please keep in mind that it's incomplete because I'm still trying to figure out exactly how to plug in my information.

The template for this script I got from other posts of people asking how to transfer variables from php to javascript using AJAX.

resources.js
$(document).ready(function{
    $.ajax({
        url : 'resources.inc.php',
        type : 'POST',
        data : data,
        success : function (){
            
        },
        error : function(){}
    })
});



Once I do successfully get the values into my Javascript file, I was thinking of how I would populate the relevant fields in my pages with the different values they need from the database.

I was thinking that I could have divs or spans or whatever on the appropriate pages with ID's and in the resources.js script I could test for those ID's and then send the needed values to those elements.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8849
  • View blog
  • Posts: 33,158
  • Joined: 12-June 08

Re: Variable storage and updating fields with AJAX

Posted 09 February 2013 - 10:47 PM

It's great to hear the book was helpful. Murach does a solid job on most of their books. (though I hope you did pay for a copy. *stern look*)

If you are still confused on the javascript to ajax to php and back again bit check that link in #2. It shows fully functioning round trips in all parts of the code.

Quote

Once I do successfully get the values into my Javascript file, I was thinking of how I would populate the relevant fields in my pages with the different values they need from the database.

Read the murach section about 'DOM'. So yes.. you would just get the div element by name and set the inner html to what ever you like!

Example - I am going to set the div tag with the id of "debug' to the string of characters that say 'empty'.
document.getElementById("debug").innerHTML = "empty"



'document' is part of the DOM call.. it searches the page for an element with the name given and throws text in it with the 'innerhtml'! Easy peasy, right?
Was This Post Helpful? 0
  • +
  • -

#9 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 31-August 12

Re: Variable storage and updating fields with AJAX

Posted 09 February 2013 - 10:54 PM

View Postmodi123_1, on 09 February 2013 - 10:47 PM, said:

(though I hope you did pay for a copy. *stern look*)


lol no worries there. My mother is an author and I'm also an aspiring author so I believe in giving other writers their due.

But you are quite correct, the book is very well written. So well written that at some point I will splurge on the PHP and HTML5 books as well.

I will take a closer look at the linked page you mentioned and get post back tomorrow.

Thanks again man! :)
Was This Post Helpful? 0
  • +
  • -

#10 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 31-August 12

Re: Variable storage and updating fields with AJAX

Posted 10 February 2013 - 10:05 AM

Ok, I looked at the link in the #2 post and compared it to my own script and tried to make some changes.

I'm not sure if I'm on the right track or not though.

$(document).ready(function(){
	$.ajax({
		url : 'resources.inc.php',
		type : 'POST',
		data : {'resources': $resource_rows},
		dataType : 'json',
		success : function (){
			
		},
		error : function (){alert("error");}		
	})	
	
});



PHP is the same as in my last post.
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8849
  • View blog
  • Posts: 33,158
  • Joined: 12-June 08

Re: Variable storage and updating fields with AJAX

Posted 10 February 2013 - 02:26 PM

Quote

I'm not sure if I'm on the right track or not though.

I am not sure how to interpret that.. does it mean it is not working, it is working, or what? Guessing at what is happening is a bit... tedious.

If your php is the same I can tell you that you are not sending any data over... the variable name in the data array needs to be the same as what you are pulling out of the $SESSION..

Aww crap... sorry I thought comment #2 was a link to a tutorial I wrote.
http://www.dreaminco...-collaboration/

From my tutorial link:

drawing.js

	    $.ajax(
	    {
	        type : "POST",
	        url : "get_id.php",
	        data :
	        {
	            "clientid" : clientid,
	            "version" : navigator.userAgent
	        },
	        dataType : "json",
	        success : function(data)
	        {
	            clientid = data;
	        }
	    });




get_id.php
	<?php
	// get relevent data
	$clientid = $_POST["clientid"];
	$version = $_POST["version"];
	// sql connection statement



Notice linkes 09 and 10? See how they line up (name wise) with lines 4 and 5? They have to be the same.
Was This Post Helpful? 1
  • +
  • -

#12 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 31-August 12

Re: Variable storage and updating fields with AJAX

Posted 10 February 2013 - 03:02 PM

Ok, I updated my script to change the name in my Json to match the name of the mysql array in the php script.

Nothing happens but I checked the Javascript Console in Google and it says that the $resource_rows is undefined. So I can only imagine that I typed something wrong either in the JSON or the mysqli query.

I also found that trying to echo the values from the php script into the html doesn't work either, says it's "undefined."

So I have to assume that it has something to do with my database query.
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3085
  • View blog
  • Posts: 10,307
  • Joined: 12-December 12

Re: Variable storage and updating fields with AJAX

Posted 10 February 2013 - 03:14 PM

<$php
should be
<?php

but I assume you may have corrected this already.

Have you tried typing resources.inc.php in your browsers' address bar (plus the necessary path) to check the json that is returned.
Was This Post Helpful? 1
  • +
  • -

#14 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 31-August 12

Re: Variable storage and updating fields with AJAX

Posted 10 February 2013 - 03:45 PM

I think I found the problem.

I changed my mysql functions over to mysqli ones on the advice of multiple people.

However I neglected to realize that the mysqli_query function takes two parameters.

I'm looking at the information on the function now and it says it needs something called a "mysqli link resource" and I have no idea what that is. Googleing now.

$resource_result = mysqli_query($resource_query);


Was This Post Helpful? 0
  • +
  • -

#15 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 31-August 12

Re: Variable storage and updating fields with AJAX

Posted 10 February 2013 - 03:53 PM

Ok, figured that one out. Had to change the functions in my connection script to mysqli as well.

resource.inc.php echoes out:

null
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »