Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Web AJAX and PHP"The Basics"

Status
Not open for further replies.
Newbie Spellweaver
Joined
Aug 25, 2008
Messages
79
Reaction score
1
Hey Guys. Was really bored and was learning AJAX for the past few days. Im just gonna whip up a small tutorial for those future web developers who are willing to use the next generation AJAX!

About AJAX​
AJAX Stands for:
Asynchronous
JavaScript
And
XML​

Basically AJAX was made popular after 2005 when Google developed its first AJAX application. You may not know it but when you type in Google there are suggestions at the bottom. This is done whenever you release the key you just pressed. Basically google has given you the ability to see the possibilities of what you have given it so far. All this is done Asynchronously. With the help of Javascript and a Server-Side scripting language (PHP, ASP etc.)

Now, this is just something to prove that AJAX was ALWAYS there. Just not used. Also client-side and server-side programming creates a hell lot of fun and ease in your websites for yourself, your server and most importantly, your users.

Requirements
Understand how to use a Server-Side Language (PHP, ASP etc...)
Little or NO Javascript knowledge (I had NONE whatsoever)
A brain that can think and do things without always needing a tutorial
To understand there is always more than 1 way.


Getting Started ~ Understanding
Now. Here is how AJAX works or can be made to work in simple terms.

1) You do something to activate a JavaScript function or similar
2) JavaScript may do a little processing here and there than send the data off for Server side processing
3) JavaScript receives the data and applys it
4) All without refreshing the page

Getting Started ~ Browser Support
There is just 1 thing about AJAX. Not every browser supports it. When I say not every browser supports AJAX i mean not every browser supports XMLHTTPObject or XMLHTTP in all. Thus we have to either find an alternative or just redirect them to a non-java-script page or something between those lines.

Now that you are done reading. Lets make our first AJAX Application (With a provided demo)

So, now that you have understood AJAX and what it truly is. Its time to get started :). Lets design our AJAX Program. I decided to teach you something really cool but i decided you wouldn't learn from it. So lets make a simple text box in which you enter a few letters, send to PHP, get the ammount of letters and display it underneath in HTML. Yes, I know this can be done completely in Javascript but I need something.

Step 1: The TextBox
Make an HTML page on your desktop or webserver (recommended) and add in a text box with the id of theTextBox. Dont know how? Than you need to brush up on your HTML skills or learn it.

So this is what we have so far
Code:
<html>
<head>
<title> First AJAX Application </title>
</head>
<input type="text" id="theTextBox" onkeyup="doFunction()" /> <!-- This is the texbox -->
<p id="numofchar">A Number Will be Given between the two tags</p>

Step 1/half: Browser Compatability And The Dog
Eh. Do I ever hate this step. We have to spend an extra 5 minutes learning Browser Compatibility. Okay so say you have a friend, who has a friend, who has another friend, who has a dog. This dog is using Internet Explorer 4. I don't think Internet Explorer 4 had XMLHTTP or AJAX ability so we need to make sure that the dog doesn't get blurred or gets a misguided web-page. So we have to tell him to upgrade his browser or just dump him at google (Dont do that, people/dogs dont like it lol).

Okay. Now to do the above you have to use JavaScript. We will check if its supported or if there is an alternative. If not we can redirect or dump. If it is supported we will set one variable for BOTH ways.

Code:
<script type="text/javascript">
      function getHTTPObject() { //  The main Function
      if (window.ActiveXObject) // For Internet explorer 5 - 6
          return new ActiveXObject("Microsoft.XMLHTTP");
      else if (window.XMLHttpRequest) // For Firefox, Chrome, Opera, Safari etc..,
          return new XMLHttpRequest();
      else { // There using an unsupported browser
          alert("Your browser does not support AJAX."); // Alert Message Telling them That there browser doesnt support Ajax.
          window.location = "http://google.ca"; // Redirect to Google lol.
          return null; // No Value to return :(
          }
      }
</script>
So in our next function when we actually do the processing we will call this function which will return the appropriate Type!

This is what we have so far

Code:
<html>
<head>

<script type="text/javascript">
      function getHTTPObject() { //  The main Function
      if (window.ActiveXObject) // For Internet explorer 5 - 6
          return new ActiveXObject("Microsoft.XMLHTTP");
      else if (window.XMLHttpRequest) // For Firefox, Chrome, Opera, Safari etc..,
          return new XMLHttpRequest();
      else { // There using an unsupported browser
          alert("Your browser does not support AJAX."); // Alert Message Telling them That there browser doesnt support Ajax.
          window.location = "http://google.ca"; // Redirect to Google lol.
          return null; // No Value to return :(
          }
      }
</script>


<title> First AJAX Application </title>
</head>
<input type="text" id="theTextBox" onkeyup="doFunction()" /> <!-- This is the texbox -->
<p id="numofchar">A Number Will be Given between the two tags</p>

Step 2: Getting the Data and Sending it for processing
Okay Now we will get the data EACH time they release a pressed key. To do that change your input to the following:
Code:
<input type="text" name="theTextBox" onkeyup="doFunction()" /> <!-- This is the texbox -->
Now we told HTML, that when they release a key execute the function "doFunction()"! so the do function will send the data to the PHP script which will echo out the result. So lets create the javascript function that will handle the request aka doFunction().

Code:
	function doFunction() {
	       ajax = getHTTPObject(); // Get the Object
	       document.getElementById('numofchar').innerHTML = 'Processing Request'; // Tell them that were processing it. Explained Later.
	       if (ajax != null) { // If it isnt null, meaning the browser does support AJAX.
			ajax.open("GET", "numchar.php?tb=" + document.getElementById('theTextBox').value, true); // Open a GET Request to numchar.php and true to be held asynchronously :)
			ajax.send(null); // Send it
			ajax.onreadystatechange = changeFunction; // When httpObject is ready for a state change meaning that it has recieved  some sort of a response. You can redirect to another function or make one		             
	       }  // They should have recieved the alert message and have been redirected.       
	}

Wow how easy was that ? The comments do explain alot. Well guess what your not done. Obviously! Time for some PHP server-side coding. Make a new file by the name of numchar.php as you specified in httpObject.open. So what do we have to do ? Basically get the data sent throught the url and then process how many characters it has and then echo it out. After that we will recieve it and then change the numofchar to the number echoed by making the function changeFunction(). So lets get started (php):
Code:
<?php
// Okay now check if they have the get statement
if (isset($_GET['tb'])) {
		// You can make this more secure by stripslashing but im lazy
		$text = $_GET['tb'];
		echo strlen($text);
		// Wow DONE :)
	}
?>

Well that was fun :D Okay now for the javascript function to change the the data. back in our HTML page lets add the javascript function changeFunction();
Code:
	function changeFunction() {
	      if (ajax.readyState == 4){ // This will be explained	
	            document.getElementById('numofchar').innerHTML = ajax.responseText; // Change the ID numofchar
	        }	
	}
Okay now lemme explain the above. First lets start with READYSTATES.
ReadyState - Discription
0 - Not even started processing
1 - Request has started and has been set-up
2 - Sent
3 - Waiting for a result
4 - Complete

So that should explain the 4. Oh and remember the processing request ? We added it there because there wouldnt really be a different from adding there and here! Now for changing the ID.
Well, for our element in HTML we gave them an ID. Now this ID is used in CSS and in Javascript. So we got the Element numofchar which was a simple paragraph and we changed its value to what we got as a response which is in httpObject.responseText.

Step 3: Putting it All Together
This is what you should have for the HTML file or PHP file.

Code:
<html>
<head>

<script type="text/javascript">
      function getHTTPObject() { //  The main Function
      if (window.ActiveXObject) // For Internet explorer 5 - 6
          return new ActiveXObject("Microsoft.XMLHTTP");
      else if (window.XMLHttpRequest) // For Firefox, Chrome, Opera, Safari etc..,
          return new XMLHttpRequest();
      else { // There using an unsupported browser
          alert("Your browser does not support AJAX."); // Alert Message Telling them That there browser doesnt support Ajax.
          window.location = "http://google.ca"; // Redirect to Google lol.
          return null; // No Value to return :(
          }
      }


	function doFunction() {
	       ajax = getHTTPObject(); // Get the Object
	       document.getElementById('numofchar').innerHTML = 'Processing Request'; // Tell them that were processing it. Explained Later.
	       if (ajax != null) { // If it isnt null, meaning the browser does support AJAX.
			ajax.open("GET", "numchar.php?tb=" + document.getElementById('theTextBox').value, true); // Open a GET Request to numchar.php and true to be held asynchronously :)
			ajax.send(null); // Send it
			ajax.onreadystatechange = changeFunction; // When httpObject is ready for a state change meaning that it has recieved  some sort of a response. You can redirect to another function or make one		             
	       }  // They should have recieved the alert message and have been redirected.       
	}

	function changeFunction() {
	      if (ajax.readyState == 4){ // This will be explained	
	            document.getElementById('numofchar').innerHTML = ajax.responseText; // Change the ID numofchar
	        }	
	}


</script>


<title> First AJAX Application </title>
</head>
<input type="text" id="theTextBox" onkeyup="doFunction()" /> <!-- This is the texbox -->
<p id="numofchar">A Number Will be Given between the two tags</p>

And for the PHP Part
Code:
<?php
// Okay now check if they have the get statement
if (isset($_GET['tb'])) {
		// You can make this more secure by stripslashing but im lazy
		$text = $_GET['tb'];
		echo strlen($text);
		// Wow DONE :)
	}
?>

Well Done. If someone could please get us a Demo that would be nice :)
I will be making more tutorials soon
Note: THERE ARE GRAMMAR ERRORS AND A FEW OTHER ONES. PLEASE COMMENT IT BELOW
~becool
Credits: Me:blush::blushing:
 
Mother effin' clouds
Loyal Member
Joined
Apr 13, 2008
Messages
1,534
Reaction score
448
Lol, wow, your slow...
P.S. Your core function is copied, so is your tutorial (more or less).
 
Newbie Spellweaver
Joined
Aug 25, 2008
Messages
79
Reaction score
1
Umm. I dont see anyone having anything the same as me. If they do I dont care because i made this myself everything from top to bottom. Some of my vocabulary is of others. I just wanted to bring out and teach those on ragezone. All i can say is that nothing that i have written is used except maybe for one of my function. Overall 75% of this is mine. Thank You. About the slow part I wanted that even those who are confused and slow to learn as well as everyone else does. I guess ive made my point

~becool
 
Last edited:
Newbie Spellweaver
Joined
Jul 14, 2008
Messages
41
Reaction score
1
Lol, wow, your slow...
P.S. Your core function is copied, so is your tutorial (more or less).

Wow Spam.. Man Its People like You that Turn RageZone in to Spamming. If you Don't Need this Tutorial Then Don't Use it Simple.... You Can At LEAST say "Good Tutorial Man" But Instead You Spam and say useless unneeded stuff..

@ BeCool. Don't worrie About that noob. Nice work on this tutorial it was we'll written and worded. It's Very Useful.


:)
 
Last edited:
Newbie Spellweaver
Joined
Aug 25, 2008
Messages
79
Reaction score
1
Thank You! I actually respected Saints. But I guess hes not that kool after all. :thumbsdown:

~becool
 
Xephizion Development
Loyal Member
Joined
Apr 19, 2008
Messages
906
Reaction score
31
Seems to be a very nice and unique Guide, but I don't have enough time to read it all..

BUT I KNOW IT WILL HELP SOMEDAY!
 
Mother effin' clouds
Loyal Member
Joined
Apr 13, 2008
Messages
1,534
Reaction score
448
Wow Spam.. Man Its People like You that Turn RageZone in to Spamming. If you Don't Need this Tutorial Then Don't Use it Simple.... You Can At LEAST say "Good Tutorial Man" But Instead You Spam and say useless unneeded stuff..

@ BeCool. Don't worrie About that noob. Nice work on this tutorial it was we'll written and worded. It's Very Useful.


:)

Welcome to the World Wide Web. I do not agree with the fact you solely classified my post as a spam. I would rather highlight the fact its a comment based on my personal opinions. There's no point saying GOOD TUTORIAL MAN without providing a rational explanation, that would be classified as spam as its unsolicited messages. I understand your intent to help, but if you can't even take such a comment, I feel sorry for you. It was nice of you to refer to me as a noob, you clearly contradicted yourself, you deserve an applaud for that. tyvm. kthnxbai.

EDIT: I lol'd when I noticed the past 2 comments against me was both staff members of VitalMS, "good" job!
 
Last edited:
Status
Not open for further replies.
Back
Top