Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to print AJAX tree

Status
Not open for further replies.

bslintx

Technical User
Apr 19, 2004
425
US
Hi all,

i'm at wits end here and hopefully someone can give me a fix action

Description: I am using serverside script and ajax to
create a tree based on user's selection.

Problem: 1. No tree is displayed(or errors) when
user selects a view.

Debug: 1. AJAX is setup to receive a custom id generated
by serverside script.
2. AJAX seems to be working correctly...the loading
icon is initiated and i used an alertbox to
show id's and all check good
3. i commented out the js script tags in
serverside CreateUnitChecklistTree()
to see if my serverside loop was woking. it
successfully printed out the varitems array
4. using simple text works fine too, so again
ajax is setup

Notes: it seems it must be the way serverside/clientside
code is printed. (order) it would be a great help
if someone knows how to setup to where i need
to assign the js when trying to print the tree
i used a dummy span tag to test the ajax...hence
why the repetitive nature of the case statement in
category_menu.asp (below)

category_menu.asp (Step 1)
this file simply collects a variable to dynamically
create an id(div(placeholder) tag name(note:yes, same name would work since using separate frames; however, it helps in modular code on serverside)) for the ajax function to send to the server response (the tree loop code)
Code:
<html>
 <head>
  <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
  <TITLE>Category Menu</TITLE>
   <script type="text/javascript" src="javascripts/ajax_menu.js"></script>
 </head>
 <body>
  <script language="JavaScript" src="javascripts/tree.js"></script>
  <script language="JavaScript" src="javascripts/tree_tpl.js"></script>
 
<%  
  sCategory= Request.QueryString("category")

  Select Case sCategory
   Case "my_profile"
    Response.Write "<span onclick=""getAjaxMenuTree('" & sCategory & "');"">Test AJAX Call</span>" & vbcrlf
    Response.Write "<div id=""my_profile""></div>" & vbcrlf
   Case "my_discrepancy"
    Response.Write "<span onclick=""getAjaxMenuTree('" & sCategory & "');"">Test AJAX Call</span>" & vbcrlf
    Response.Write "<div id=""my_discrepancy""></div>" & vbcrlf
   Case "unit_discrepancy"
    Response.Write "<span onclick=""getAjaxMenuTree('" & sCategory & "');"">Test AJAX Call</span>" & vbcrlf
    Response.Write "<div id=""unit_discrepancy""></div>" & vbcrlf
   Case "unit_checklist"
    Response.Write "<span onclick=""getAjaxMenuTree('" & sCategory & "');"">Test AJAX Call</span>" & vbcrlf
    Response.Write "<div id=""unit_checklist""></div>" & vbcrlf
   Case "my_checklist"
    Response.Write "<span onclick=""getAjaxMenuTree('" & sCategory & "');"">Test AJAX Call</span>" & vbcrlf
    Response.Write "<div id=""my_checklist""></div>" & vbcrlf
  End Select   
%>
 
 </body>
</html>

ajax_menu.asp (Step 2)
this file grabs the div id and send to asp to process
Code:
var xmlHttp
var menu_id
var my_id
function getAjaxMenuTree(menu_id)

{ 
  
 my_id = menu_id

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
} 

var url="server_response_tree.asp"

url=url+"?menu_id="+menu_id
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
 
}

function stateChanged() 
{ 
 
if(xmlHttp.readyState == 1)
{
 
 document.getElementById(my_id).innerHTML = "Loading Menu...<img src='./images/loader3.gif'>";
}

if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 

document.getElementById(my_id).innerHTML=xmlHttp.responseText

} 
} 

function GetXmlHttpObject()
{ 
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}

server_response_tree.asp (Step 3)

this file collects the div placeholder id received from ajax_menu via querystring (get)...and also is where i am trying to loop the vartree items to populate the javascript function call- placed at the bootom...all ascociated js files are placed for the tree
Code:
  <!-- #include file = "procedures/ConnectDatabase.asp" -->
  <!-- #include file = "procedures/GetLogon.asp" -->  
  <!-- #include file = "procedures/CreateTreeNodes.asp" --> 
  <!-- #include file = "procedures/CreateTreeFramework.asp" -->
  <!-- #include file = "procedures/CreateMyDiscrepancyTree.asp" -->
  <!-- #include file = "procedures/CreateUnitDiscrepancyTree.asp" -->
  <!-- #include file = "procedures/CreateUnitChecklistTree.asp" -->
  <!-- #include file = "procedures/CreateMyChecklistTree.asp" -->

<%
  Dim oConn

  ConnectDatabase "USAF_SIP"

  

  sMenuID= Request.QueryString("menu_id")

  If sMenuID = "unit_checklist" Then
   CreateUnitChecklistTree
   Response.Write "<script language=""JavaScript"">" & vbCrlf & _
                  "new tree (TREE_ITEMS, TREE_TPL);" & vbCrlf & _
	          "</script>" & vbCrlf
  End If


   
%>







 
sorry...in case this is needed
CreateUnitChecklistTree.asp
Code:
<%
  Sub CreateUnitChecklistTree()
   Set oRs = oConn.EXECUTE("sp_USAF_Discrepancy_Header_2 '" & GetLogon & "'")
    If oRs.EOF Then
     Response.Write "Error: User not in database"
    Else
     iUnitSectionID = oRs("USAF_UnitSection_RecID")
     sSquadron = oRs("USAF_Squadron_Abbreviation")
     sFlight = oRs("USAF_Flight_Symbol")
     sOfficeSymbol = oRs("USAF_Section_Name")
    End If
     
    If LEN(sFlight) > 0 Then
     Set oRs = oConn.EXECUTE("sp_Lookup_Count_Checklist '" & sFlight & "'")
     If Not oRs.EOF Then 
      iChecklistCount = oRs("Checklist_Count")
     Else
      Response.Write "Error: No count"
     End If
    End If
                  
      
    If LEN(sFlight) > 0 AND LEN(GetLogon) > 0 Then
     Response.Write "<script>" & vbCrlf & _
                    "var TREE_ITEMS = [" & vbCrlf 

      Response.Write "['Unit Checklists (" & sSquadron & ")', null," & vbCrlf

      
      Response.Write "['All', null," & vbCrlf 

  
     Set oRs = oConn.EXECUTE("sp_Lookup_Status_Menu_Squadron_Name_All '" & sSquadron & "'")
     If Not oRs.EOF Then
      aFlightTree = oRs.GetRows
      oRs.Close
      Set oRs = Nothing

      For iFlightCounter = 0 to UBOUND(aFlightTree,2)
       Response.Write "['" & aFlightTree(1,iFlightCounter) & "', null," & vbCrlf 
        Set oRs = oConn.EXECUTE("sp_Lookup_Status_Menu_Flight_Name_All '" &  sSquadron  & "','" & aFlightTree(1,iFlightCounter) & "'")
        If Not oRs.EOF Then
         aSectionTree = oRs.GetRows
         oRs.Close
         Set oRs = Nothing         
         For iSectionCounter = 0 to UBOUND(aSectionTree,2)
          Response.Write " ['" & aSectionTree(2,iSectionCounter) & "',null," & vbCrlf 
          Set oRs = oConn.EXECUTE("sp_Lookup_Status_Menu_Section_Name_All '" &  sSquadron  & "','" & aFlightTree(1,iFlightCounter) & "','" & aSectionTree(1,iSectionCounter) & "'")
           If Not oRs.EOF Then
            aFunctionalAreaTree = oRs.GetRows
            oRs.Close
            Set oRs = Nothing
            For iFunctionalAreaCounter = 0 to UBOUND(aFunctionalAreaTree,2)
             Response.Write " ['" & aFunctionalAreaTree(4,iFunctionalAreaCounter) & "',null," & vbCrlf 
             Set oRs = oConn.EXECUTE("sp_Lookup_Status_Menu_FunctionalArea_Name_All '" &  sSquadron  & "','" & aFlightTree(1,iFlightCounter) & "','" & aSectionTree(1,iSectionCounter) & "'," & aFunctionalAreaTree(3,iFunctionalAreaCounter))
              If Not oRs.EOF Then
               aFunctionalAreaOfResponsibilityTree = oRs.GetRows
               oRs.Close
               Set oRs = Nothing
               For iFunctionalAreaOfResponsibilityCounter = 0 to UBOUND(aFunctionalAreaOfResponsibilityTree,2)
                Response.Write "    ['" & aFunctionalAreaOfResponsibilityTree(6,iFunctionalAreaOfResponsibilityCounter) & "']," & vbCrlf 
               Next
               Else
                Response.Write "['No Functional Area of Responsibilities'],"
              End If
             Response.Write "]," & vbCrlf
            Next
           Else
            Response.Write "['No Functional Areas'],"
           End If
          Response.Write "   ]," & vbCrlf
         Next
        Else
         Response.Write "['No Sections'],"
        End If   
        Response.Write "  ]," & vbCrlf  
      Next
     Else
      Response.Write "['No Flights'],"
     End If           
        Response.Write "]," & vbCrlf

            Response.Write "]" & vbCrlf 
         Response.Write   "];" & vbCrlf 
     Response.Write "</script>" & vbCrlf
    End If
   End Sub
%>
 
[1] Add a defer property to the script.
>Response.Write "<script>" & vbCrlf & _
"var TREE_ITEMS = [" & vbCrlf

[tt]Response.Write "<script [blue]defer[/blue]>" & vbCrlf & _
"var TREE_ITEMS = [" & vbCrlf [/tt]

[2] Use insertAdjacentHTML method for ie or add an head prototype mimicking the same for moz and then use it. A prototype can be found here for instance ( >document.getElementById(my_id).innerHTML=xmlHttp.responseText
[tt]document.getElementById(my_id).insertAdjacentHTML("beforeEnd",xmlHttp.responseText);[/tt]

[3] You've to put some sort of id to the inserted script tag. If the repetitive call to the same menu, you can more easily avoid piling up the insertions. You can remove the child of that particular id and then insert it after.

There may be more problems than just the above. But those would probably be related to different things than the main functionality layout.
 
thanks so much for replying tsuji...

unfortunately, i still have not been able to get this to work...i did 1 and 2 on your suggestions

1. no change
2. infinite loop...kept up loading message
3. i didn't understand 3.

i openened up a dom inspector and saw that the script tag was not printed. i finally got that to work by inserting a serverside write statement above it....i don't understand that one. anyways, now i have the full code showing under the dom inspector...however...it's not painting the tree in the browser. i copied and pasted the dom inspector rendered html and statically put in an html page...worked as advertised. so...although i got the code to be returned by the server correctly...it does not paint on an ajax call to the div placeholder. any server text comes up..put no javascript.


anymore ideas...i am at a loss here...i thought for sure it would be easy...but i have underestimated it...any ideas would be greatly appreciated. i noticed there is not an upload function on this forum. i created an extremely lightweight folder to demo the problem. it's much tinier than the code represented above. guess i should have done that considering only one hit on this problem. if anyone is interested in trying to resolve this w/ my file...please let me know and i will email it. again, any help is greatly appreciated
 
>1. no change
No change may it be. But I do not need confirmation of it working to assure you you have to put it there. So my advice is you stick with it. It is up to you.

>2. infinite loop...kept up loading message
This I don't understand. Do you mean if you use .innerHTML, it does not loop? I don't believe it, but I don't have crystal ball.

>3. i didn't understand 3.
If you insert html node to the my_id container each time you make an remote call, the node will repeatedly inserted. That's why you have to look for that script node (and with an id, it is easier to look for it), and if it is already there, remove it using removeChild method before inserting the response containing the node anew.

There is another repeated call detail you have to take care of.
[4] After the responseText is retrieved, one way or another, you practically _must_ dump the reference, again one way or another. You can do it like this.
[tt]xmlHttp=null;[/tt]
For next call, create the object anew.

I don't know the my_id structure in action, but to guard against all odds, you can make the response like this.
[tt]
Response.Write "[blue]<div id=""xmlhttp_id""><span style=""display:none;"">&nbsp;</span>[/blue]" & _
"<script [blue]defer[/blue]>" & vbCrlf & _
"var TREE_ITEMS = [" & vbCrlf
[green]'etc etc fill in the additional message until completed[/green]
[blue]Response.write "</div>" 'close the div out[/blue]
[/tt]
The id I am talking about in [3] is reflected then in the xmlhttp_id here.

To make thing work, any other lines and serverside include should be integrated haromously into the whole. That I have no gurantee.
 
sorry for not clarfifying what i had done...i rushed it..


and to say-the-least...it's GREATLY appreciated for the help...it's a little wakward for me to ask for help..i usually figure it out..and not working with client side has definitely opened my eyes ....i simply didn't "need" to know until now...it's always been serverside...and "limited/standard" client stuff...so again thanks


...

1. no change meant i included the defer..yet no change
2. infinite loop ...at the time i was viewing the "loading" icon and ummm...the word that came to mind was infinite loop because it didn't go way..yes, poor choice of words i guess...but techically it IS looping on the asp side...to get the javascript elements..guess it's how you look at it...bottom line it was "stuck" here
3. ok, before you say ...go to javascript how to's,:D...is this not already done by placing a unique id in the div placeholder? the javascript knows "where" to put the rendered html code...it simply not drawing out the tree

i have started a thread in an asp forum...it has a zip file in the first post that emulates the problem AND what the script does if shown

i am going to try your latest fix actions ...and will certainly come back w/ the results...hopefully w/ your js skills i can get this nipped in the bud...it's been frustrating for me and your patience has been appreciated!

here is the link to the asp thread...perhaps you could take a look at the code uploaded....and see what's going on firsthand....i find obviously that active source code is obviously the choice of debugging vs an amateur scripter trying to relay the problem :D...thanks

 
Hi bslintx. Your reponse posts have nothing wrong. I only wanted to talk straight. On points I have no doubt, I make the points without ambiguity hoping to get the message through. On points I am in no position or time to disperse the cloud, I try to be conditional. I may not succeed doing that every time. Hence, I hope you don't feel offended. Actually, from what you post you know the subject better than many, and do frequent at time to help out others here for topic you feel comfortable with.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top