SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ajax returnng info twice

    I have an ajax application where there are two functions using the same RequestObject and the same URL. The following code means nothing but illustrates my problem. The first function loads as an onLoad event and returns a table cell with an onclick event for the second function. However when that cell is clicked it does what it is supposed to (echos Hullo) but also returns another unwanted tablecell. Do I have to use separate Request objects so that this doesn't happen or can anyone see why this is happening?
    I know I can avoid it by using
    Code:
    <?php
    if(isset($_GET['detail'])){
    echo $detail;
    }
    ?>
    on the php page but I don't want to do it that way as it interferes with other things I want to do.
    The code for the ajax page is
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script>
    
    			 
    function navigate(detail) {
            var url = "testpage.php?detail="+detail;
    		var callback ="callback1";
    		getData(url, callback);
     }
     
     
     
     
    function toggle() {    
    		var arr="Hullo";
             var url = "testpage.php?act="+arr;
    		 var callback ="callback2";
    		 getData(url, callback);
    }		  
    
    
    //general ajax
    function getData(url, callback)
    { 
      var XMLHttpRequestObject = false; 
    
      if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new 
         ActiveXObject("Microsoft.XMLHTTP");
      }
    
      if(XMLHttpRequestObject) {
        XMLHttpRequestObject.open("GET", url, true); 
    
        XMLHttpRequestObject.onreadystatechange = function() 
        { 
          if (XMLHttpRequestObject.readyState == 4 && 
            XMLHttpRequestObject.status == 200) { 
    		var response = XMLHttpRequestObject.responseText;
    		
    		
    		if(callback=="callback1" ){
             callback1(response);
    		
    		 }
    		 
    		 if(callback=="callback2"){
    		 callback2(response);
    		 } 
    		
              delete XMLHttpRequestObject;
              XMLHttpRequestObject = null;
          } 
        } 
    
        XMLHttpRequestObject.send(null); 
      }
    }
    //callback functions
     function callback1(text)
          {
            document.getElementById("targetDiv").innerHTML = 
             text;
          }
    function callback2(text)
          {
            document.getElementById("dater").innerHTML = 
             text;
          }
    
    	  
    </script>
    </head>
    
    <body onLoad='navigate(""); '>
    
     
    
    <div id="targetDiv" align="center"></div>
      <div id="dater" align="center"></div>
        
       
     
    	
    </body>
    </html>
    and the code for testpage.php is
    Code:
    <?php 
    $arr=$_GET['act'];
    echo $arr;
    
    $detail=$_GET['detail'];
    if($_GET['detail']== '') {
    $detail="<table border='1'><tr><td onclick='toggle()>TEST</td></tr></table>";
    }
    
    echo $detail;
    
    ?>
    Thanks!!

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know I can avoid it by using..
    If you can avoid making xhr requests, you should do so at all costs. .

    The first function loads as an onLoad event and returns a table cell with an onclick event for the second function. However when that cell is clicked it does what it is supposed to (echos Hullo) but also returns another unwanted tablecell.
    Use this php and your javascript will work:
    PHP Code:
    <?php

    if( isset($_GET['detail']) )
    {
        if( 
    $_GET['detail'] == '' )
            echo 
    "<table border='1'><tr><td onclick='toggle()'>TEST</td></tr></table>";
    }
    else if (isset(
    $_GET['act']) )
    {
        echo 
    "Hello";
    }

    ?>
    Last edited by 7stud; Jan 20, 2007 at 01:36.

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I actually meant that I can avoid the doubling up by changing the php. Why do you say that anyway?

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then this:
    Code:
    $arr=$_GET['act'];
    will always cause a php error as soon as the page loads. And, this:
    Code:
    $detail=$_GET['detail'];
    will always cause a php error when you click on the link.

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help but I think I mentioned that I had a soution by changing the php. I was hoping to be able to change the javascript to fix the problem.
    Why will
    Code:
    $arr=$_GET['act'];
    and
    Code:
    $detail=$_GET['detail'];
    always cause problems??

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because the first time the php page is called 'act' is undefined, so $_GET['act'] causes an error; and the second time the php page is called 'detail' is undefined, so $_GET['detail'] causes an error.


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
  •