Get a variable back with ajax

Posted on 2009-02-21
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"

Any help would be appreciated.
server has:

echo "<?xml version=\"1.0\" encoding=\"UTF-8\" 



js is:

	var ajaxRequest;  // The variable that makes Ajax possible!



		// Opera 8.0+, Firefox, Safari

		ajaxRequest = new XMLHttpRequest();

	} catch (e){

		// Internet Explorer Browsers


			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");

		} catch (e) {


				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);




}"GET", ""+posx+"&ypos="+posy, true);



Open in new window

Question by:cubbyboy57
    LVL 3

    Accepted Solution

    You should look into using JQuery:
    And more specifically using JSON.
    Here's a quick example:

    Author Comment

    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.
    LVL 2

    Assisted Solution

    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"

    Open in new window


    Author Closing Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
    JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
    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…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now