Jump to content

AJAX


astralaaron

Recommended Posts

What you need to understand is that XMLHttpRequest() does one very simple thing, and that is to send an HTTP request (despite XML being included in its name). Whatever you can do with plain HTTP, you can do with XHR. The only "special" thing about XML in all of this is that with the "responseXML" property, if your response is an XML document (or should I say, if the browser can read it as such), you can directly get a DOM tree representation of it. But even if it's not, "responseText" can still contain the raw data, regadless of what it is. It could (in theory) even be an image.

Link to comment
Share on other sites

If you mean using GTalk in your Gmail, that involves Gmail sending very small pings to itself in incredibly small itervals(a second or less) these requests are often just a check to update the database in Gmail to tell you who's online/offline. The result from the consequent pings of your friends from your gmail indicate the online. Think of it like this:1. You are already logged into gmail and your friend Bob is not.2. Bob logs into his computer, goes to gmail. They set their database system to "active" for bob.3. Gmail sends a ping to its server to see who, if any, of your friends is online.4. The server returns "online" for bob, so the image next to his name turns into the green circle.5. You now know bob is on.The same process happens when either you or bob sign off.

Link to comment
Share on other sites

If you mean using GTalk in your Gmail, that involves Gmail sending very small pings to itself in incredibly small itervals(a second or less) these requests are often just a check to update the database in Gmail to tell you who's online/offline. The result from the consequent pings of your friends from your gmail indicate the online. Think of it like this:1. You are already logged into gmail and your friend Bob is not.2. Bob logs into his computer, goes to gmail. They set their database system to "active" for bob.3. Gmail sends a ping to its server to see who, if any, of your friends is online.4. The server returns "online" for bob, so the image next to his name turns into the green circle.5. You now know bob is on.The same process happens when either you or bob sign off.
i understand the concept, I have learned and memorized how to create an XMLHttpRequest() or ActiveXObject()and have a basic understanding that there are 6 methods and 6 properties within them.. I wrote them down - haven't memorized them but am formilliar.but I have only done a simple hello world tutorial where you click a link and load some text into a <span></span> from an html file.I tried doing the same with a php file but it failed... anyway I am just wondering how to send variables back and forth from a php file using AJAX.**I made my own makeshift version of gmails online check using html frames / meta refresh and php hah - it works :)
Link to comment
Share on other sites

... anyway I am just wondering how to send variables back and forth from a php file using AJAX.
You could use Session vars.BTW; this might be out of topic but, could anyone tell me how is "AJAX" pronounced?? I don't wanna make a new thread asking that :)
Link to comment
Share on other sites

ok I realized the reason why I couldn't do the same thing with the php file is that i needed the file I was pulling the php into to be a .php file... makes sense.....how do you send variables with the XMLHttpRequest send() to a php file ?

Link to comment
Share on other sites

You make the URL that you are requesting have a query string:

var url = "mypage.php?var1=somevalue&var2=someothervalue";

Then you parse that query string in the PHP to get the values.

Link to comment
Share on other sites

how do you send variables with the XMLHttpRequest send() to a php file ?
The send() method is used with a parameter when you use the POST method in the xmlHttp.open().If you're sending the variables through the get method, then do what jesh said, and the send() parameter is null.Example:
var url="gethint.php"url=url+"?q="+strurl=url+"&sid="+Math.random()xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true)xmlHttp.send(null)

Is it just me or did he leave off the X?"ay-jaa"
It's just you
Link to comment
Share on other sites

For most useful AJAX, you will be using the POST method and WILL be sending some data to your script in the send statement.Open opens the connection.Read the returning data in the function that onreadystatechange points to. THIS relationship is what makes the whole process asynchronous.

Link to comment
Share on other sites

so how do you pass them through with the send?
Just as you'd expect:$myDATA = getMyData(); \\ you write this routine, or pull the data from an array, whateverxmlHttp.send($myDATA);And just that simple.
Link to comment
Share on other sites

It involes setting a header. Off the top of my head, i believe the header is content-type. What that value is i don't know off top, something about x-form-urlencoded. Something along those lines.

var xml = new XMLHttpRequest();xml.open('POST','yourPage.php');xml.onreadystatechange=function(){   if(xml.readyState==4){	  document.write(xml.responseText);   }};xml.setHeader('content-type','x-form-urlencoded');xml.send('user=bob&pw=jigglypuff');//This is yourPage.php:<?php  echo $_POST['user'];  echo "<br />".$_POST['pw'];?>//In our example this would output bob<br />jigglypuff

Link to comment
Share on other sites

The line would be like this:xml.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
Hey, i was close, especially considering i had no idea what it was off the top of my head. I just forgot application and -www- between the x and form. hehe. I found out awhile back that content-type doesn't have to be in that form. It could be CONTENT-TYPE and it would still work. Browser's don't really care as long as its there.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...