?
Solved

Get a variable back with ajax

Posted on 2009-02-21
4
Medium Priority
?
368 Views
Last Modified: 2012-05-06
I  need to get a variable back from the server when I send an ajax request. (using PHP).
I was able to get the text back so pretty sure the requesting part is ok. I read that js can't assign any variable to a responseText. If this is not true I could use that.
I thought I would try to use xml, but not sure how to format the xml.  And not sure I have everything to get the xml.  

 I keep getting xml doc is null.

Here is what I see when I view the source of the page I am sending the request to:
<?xml version="1.0" encoding="UTF-8"
  standalone="yes"?><message><type>0</type><code>00</code></message>


Any help would be appreciated.
server has:
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" 
  standalone=\"yes\"?>".
$returnmessage;
 
js is:
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
ajaxRequest.onreadystatechange = function(){
if (ajaxRequest.readyState == 4) {
    if (ajaxRequest.status == 200) {
      var xmlDoc = ajaxRequest.responseXML;
      var showElements = xmlDoc.getElementsByTagName("message");
      for (var x=0; x<showElements.length; x++) {
        var type = showElements[x].childNodes[0].value;
        var code = showElements[x].childNodes[1].value;
        alert(type+" & code "+code);
      }
    }
  }
}
ajaxRequest.open("GET", "http://mysite.com/message.php?xpos="+posx+"&ypos="+posy, true);
ajaxRequest.send(null); 
}

Open in new window

0
Comment
Question by:James Stone
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
Blomholm earned 1000 total points
ID: 23701004
You should look into using JQuery: http://jquery.com/
And more specifically using JSON.
Here's a quick example: http://www.prodevtips.com/2008/08/15/jquery-json-with-php-json_encode-and-json_decode/
0
 

Author Comment

by:James Stone
ID: 23701104
I am working with a game and I am trying to determine if they click on a image if is (a) valid and (b) what happens. It was all written in php and works fine, but the page refreshes after every click.

I am trying to send the location of the click to php and return a message and then based on that message get js to do something.  I don't want to have the results of the first request visible at all.

I am trying to find an example of where I send a ajax request (I don't care how) and the php result would get 1 added to it by js before it is displayed.  I think I could work from there. I am only working with numbers.
0
 
LVL 2

Assisted Solution

by:VoxEcho
VoxEcho earned 1000 total points
ID: 23703588
if you are seeing what you want to see in the responseText then you should be able to use that:

if(ajaxRequest.responseText == 1){
// do something;
}

you can't change the responseText property, but you sure can look at it. on the other hand if you want to use the XML you have to make sure that the script which is creating the XML and setting the content type - else Javascript will not see it as actual XML and wont let  you parse it.



<?
header("Content-type: text/xml");
?><?xml version="1.0" encoding="UTF-8"
  standalone="yes"?><message><type>0</type><code>00</code></message>

Open in new window

0
 

Author Closing Comment

by:James Stone
ID: 31549612
thanks for the info, I finally figured out that the end of the responseText has a return or whitespace or something that keeping me from using it in a function.
Also that you can *1 to get the text into a number
I will look at JSON more if I have alot of AJAX to do (have this code working now!)
thanks again for the info - it got me going in the right direction.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question