Reply to topic
PHP and AJAX
darnold


Joined: 17 Mar 2005
Posts: 30
Reply with quote
This is a really simply tutorial so you can integrate your php with ajax. The example code utilizes a universal function called get. You can pass the get function the php file name to execute, the div id where the results are to be displayed, and 2 url variable values for use in your php. The names of those variables are $_GET['x'] and another called $_GET['y']. Use null for the div id if the php results are not to be displayed. You can also set x and y to null if you don't need them.

Create ajax.html then copy and paste this code:

Code:
<html>
<head>
<script src="js/ajax.js"></script>
</head>

<body>

<a href="javascript:get('date','displayID',null,null)">Display Date</a>

<div id="displayID"></div>

</body>
</html>


In this example I used a general html link to execute the javascript get function. Javascript functions can also be called from onClick, onChange, and several other methods.

Create the ajax.js file in a folder called js. Copy and paste this code.
Code:

var xmlHttp
var divId
//get menthod parameters
//file = file name without the php extension
//div = the block level div id displaying the results
//x = get x
//y = get y
function get(file,div,x,y)
{
divId = div
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
   
//define page to run
var url=file+".php";
url=url+"?sid="+Math.random();
url=url+"&x="+x+"&y="+y;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}


function stateChanged()
{

   if (xmlHttp.readyState==4)
   {
      //test text = resulting output
      text = xmlHttp.responseText
      //write to div
      if(divId!=null)
         {
         document.getElementById(divId).innerHTML = text
         }
   }
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}




Create date.php, then copy and paste this code:
Code:


<h2>File contents</h2>
<hr />
<?php

echo date('M d,y');


?>

<h2>Get array results</h2>
<hr />
<pre>
   <?=print_r($_GET)?>
</pre>


Browse to ajax.html and click.

That was easy...
Buzz Net Marketing


Joined: 19 Apr 2009
Posts: 3
Reply with quote
Even Easier ....

http://www.prototypejs.org/

Wink
nathacof
Forum Admin

Joined: 24 Oct 2006
Posts: 192
Location: Dover, DE
Reply with quote
Don't limit yourself ...

http://code.google.com/apis/ajaxlibs/

Wink
Zoran Bojan


Joined: 08 May 2009
Posts: 3
Reply with quote
Hi,

AJAX (Asynchronous Javascript And XML) is a collection of technologies which allow a script to communicate with a server in real time. There is a lot of history behind it, almost all of which is beyond the scope of this article, but I’d like to start this series by introducing you to the power of AJAX.
The most important thing to remember about AJAX is that it is server independent, which means you can use any server side language you please. For these examples we will use PHP; and if you have mastered basic JavaScript, you can start immediately.
PHP and AJAX
Zoran Bojan


Joined: 08 May 2009
Posts: 3
Reply with quote
Ajax, sometimes written as AJAX (shorthand for asynchronous JavaScript and XML), is a group of interrelated web development techniques used to create interactive web applications or rich Internet applications. With Ajax, web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page. The use of Ajax has led to an increase in interactive animation on web pages. Data is retrieved using the XMLHttpRequest object or through the use of Remote Scripting in browsers that do not support it. Despite the name, the use of JavaScript and XML is not actually required, nor do the requests need to be asynchronous.

Games development
PHP and AJAX
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic