Results 1 to 7 of 7

Thread: AJAX interaction

  1. #1
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Exclamation AJAX interaction

    Hi guys, so i posted a thread here a couple of days ago about getting the id of a link that was clicked to a javascript function. that was answered in minutes. great

    but i have another question or problem.

    i have this img
    HTML Code:
    <a href="#" onClick="ajaxFunction(this.id)" id="volcano"><img src="images/lines.jpg" /></a>
    and this links to the typical ajax script that checks the browser and then sends the query.
    Code:
    function ajaxFunction(id) {
    	var xmlHttp;
    	try {
        	// Firefox, Opera 8.0+, Safari
        	xmlHttp=new XMLHttpRequest();
    	} catch (e) {
        	// Internet Explorer
        	try {
    			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        	} catch (e) {
          		try {
            		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
          		} catch (e) {
            		alert("Your browser does not support AJAX!");
            		return false;
          		}
        	}
      	}
    	xmlHttp.onreadystatechange=function() {
    		if(xmlHttp.readyState==4) {
            	var ajaxDisplay = document.getElementById('ajaxDiv');
    			ajaxDisplay.innerHTML = xmlHttp.responseText;
          	}
        }
    	
        var queryString = "?type=" + id;
        xmlHttp.open("GET", "ajax.php" + queryString, true);
        xmlHttp.send(null);
    }
    </script>
    so when i get to the ajax.php and try to print out the results of an sql query it doesnt seem to do so. I mean when i click on the link in my page it empties the ajaxDiv (cos i have something in there already, which i want to replace) but doesnt put anything in it. i cant even seem to get an echo to come out that is at the top of the ajax.php script....

    any help please?
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  2. #2
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    Doesnt matter! I was being an idiot! and i hadnt put a curly bracket to end in the php backend bit so it was completely throwing me. All together now, 'You absolute idiot ' :P
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  3. #3
    Join Date
    Apr 2008
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Hi I am having a similar problem.. this is my first time using the xmlhttprequest and i was following an example in a book and still cant get it to work.
    i am using mozilla 2.0.0.13 . and firebug to debug my script. The serverPage that I am using only has the words Hello World in it. nothing else.
    In debug mode i get as far as the IF statement. It never enters into the IF statement. If I hit continue a few times and mouseover .responsetext it does show the contents of the file in the serverPage variable, but it never gets to assign it to the div.

    I would appreciate all help..
    thanks.

    function getProductDetails(serverPage, objID)
    {
    var httpRequest;
    if (window.XMLHttpRequest)
    { // Mozilla, Safari, ...
    httpRequest = new XMLHttpRequest();

    } else if (window.ActiveXObject)
    { // IE
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var obj= document.getElementById(objID);
    httpRequest.onreadystatechange = function()
    {
    if (httpRequest.readystate == 4)
    {

    obj.innerHTML = httpRequest.responseText;
    }
    }
    httpRequest.open("GET", serverPage, true);
    httpRequest.send(null);
    }

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Can you post a link to the problem page, or at least show how you are calling this script (more details please).
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Apr 2008
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    This is the full text. it is just a test page .

    I've put breakpoints at various points in the program.
    The program first creates the XMLHttpRequest
    Then initializes the obj variable.
    Then the httpRequest.onreadystatechange
    Then httpRequest.open("GET", serverPage, true);
    Then begins the If statement.. When i hover over the readystate it shows as undefined.
    Then it goes down to the .send(null) line and then back to the if statement.
    when i hit continue in the debugger it stays at the if statement. i hit continue again and it still stays at the if statement, but if i hover over the .responestext it shows that the response is loaded with the file that i am trying to get.
    But it never assigns the value to the div and it the program doesnt actually go into the if statement to assign the div.

    i hope this makes some sense. As far as the way it jumps around and does not follow the sequence of the statements., is this normal AJAX behavior?



    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>AJAX Test </title>

    <script type="text/javascript">
    function getProductDetails(serverPage, objID)
    {
    var httpRequest;
    if (window.XMLHttpRequest)
    { // Mozilla, Safari, ...
    httpRequest = new XMLHttpRequest();

    } else if (window.ActiveXObject)
    { // IE
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var obj= document.getElementById(objID);
    httpRequest.onreadystatechange = function()
    {
    if (httpRequest.readystate == 4)
    {

    obj.innerHTML = httpRequest.responseText;
    }
    }
    httpRequest.open("GET", serverPage, true);
    httpRequest.send(null);
    }

    </script>
    <link rel="stylesheet" type="text/css" href="design.css"/>

    </head>

    <body>
    <div id='container' onclick="getProductDetails('productdetails.html','container');">
    This is my container


    </div> <!-- End Container-->


    </body>
    </html>

  6. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Something that i completely overlooked was this line:

    Code:
    httpRequest.onreadystatechange = function()
    {
    if (httpRequest.readystate == 4)
    readystate should be readyState (capital S). After that change the code works fine.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  7. The Following User Says Thank You to thetestingsite For This Useful Post:

    raman19 (04-15-2008)

  8. #7
    Join Date
    Apr 2008
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Wow.. its always the little things..

    Thank you so much for your time. It's really appreciated..

    I would have gone on for days without realizing it.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •