472,110 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

ASP and AJAX problems

Hello,

I am having problems with using AJAX to call information to my primary ASP page from a secondary asp page that brings in the data I want to display.

I'm having the onfocus event trigger the function show() to call in the data from the secondary asp page. However, when I click on the record in the list, nothing happens. The information is suppose to appear ni the div id = "div_id". Please help. I need the information in the secondary asp page to appear in the primary asp page.

Relevant part of primary asp page:

Expand|Select|Wrap|Line Numbers
  1. <table width="786" height="215" border="0" cellspacing="0">
  2.   <tr>
  3.     <td width="183" height="213" align="center" valign="top">
  4.     <table width="183" height="196" border="0" cellspacing="0">
  5.       <tr>
  6.         <th height="27" bgcolor="#0099FF" align="center">&nbsp;</th>
  7.       </tr>
  8.       <tr>
  9.         <th height="12" bgcolor="#0099FF" align="center">List Heading</th>
  10.       </tr>
  11.       <tr>
  12.         <td valign="top" align="center"><form id="form3" name="form3" method="get">
  13.         <select name="selectlist" size="10" id="list"onfocus="show(this.value)">
  14.           <%while not rsS.eof%>
  15.           <option id="<%=rsS("ID")%>"><%= rsS("Name") %></option>
  16.           <% rsS.movenext
  17.              wend%>
  18.         </select>
  19.         </form></td>
  20.       </tr>
  21.     </table>
  22.     </td>
  23.     <td width="486" valign="top"><table width="597" border="1" cellspacing="0">
  24.       <tr>
  25.         <th colspan="4" scope="col" bgcolor="#0099FF">Display Heading 1</th>
  26.         <th scope="col" bgcolor="#0099FF">Display Heading 2</th>
  27.       <tr>
  28.         <th width="9%" height="23" bgcolor="#0099FF"><em>Column1 Heading</em></th>
  29.         <th width="9%" bgcolor="#0099FF"><em><strong>Column2 Heading</strong></em></th>
  30.         <th width="30%" bgcolor="#0099FF"><em><strong>Column3 Heading</strong></em></th>
  31.         <th width="26%" bgcolor="#0099FF"><em><strong>Column4 Heading</strong></em></th>
  32.         <th width="26%" bgcolor="#0099FF">&nbsp;</th>
  33.       </tr>
  34.     </table><div id="div_id"></div>
  35.     </td>
  36.   </tr>
  37. </table>

JavaScript (js) page

Expand|Select|Wrap|Line Numbers
  1. // JavaScript Document
  2.  
  3. var xmlHttp
  4.  
  5. function show(str)
  6. xmlHttp=GetXmlHttpObject();
  7. if (xmlHttp==null)
  8.   {
  9.   alert ("Your browser does not support AJAX!");
  10.   return;
  11.   } 
  12. var url="primaryasppage.asp";
  13. url=url+"?variable="+str;
  14. url=url+"&sid="+Math.random();
  15. xmlHttp.onreadystatechange=stateChanged;
  16. xmlHttp.open("get",url,true);
  17. xmlHttp.send(null);
  18. }
  19.  
  20. function stateChanged()
  21. if (xmlHttp.readyState==4)
  22. document.getElementById("div_id").innerHTML=xmlHttp.responseText;
  23. }
  24. }
  25.  
  26. function GetXmlHttpObject()
  27. {
  28. var xmlHttp=null;
  29. try
  30.   {
  31.   // Firefox, Opera 8.0+, Safari
  32.   xmlHttp=new XMLHttpRequest();
  33.   }
  34. catch (e)
  35.   {
  36.   // Internet Explorer
  37.   try
  38.     {
  39.     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  40.     }
  41.   catch (e)
  42.     {
  43.     xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  44.     }
  45.   }
  46. return xmlHttp;
  47. }
Here is the secondary asp page code.

Expand|Select|Wrap|Line Numbers
  1. <% 
  2. Set rs = Server.CreateObject("ADODB.Recordset")
  3.         rs.ActiveConnection = "dsn=dsn;uid=user;pwd=pwd;database=db;"
  4.         rs.CursorType = 0
  5.         rs.CursorLocation = 3
  6.         rs.LockType = 1
  7.         rs.Source = ("Stored Procedure @SQLVariable= '" & request.QueryString("variable") & "'")
  8.         rs.Open()
  9.  %>
  10. <% while not rs.eof%>
  11. <% response.Write("<tr>") %>
  12. <% response.Write("<td>") %><%= rs("column1") %><% response.Write("</td><td>")%><%= rs("column2") %><% response.Write("</td><td>")%><%= rs("column3") %> <% response.Write("</td><td>")%><%= rs("column4") %><% response.Write("</td><td> &nbsp; </td>")%>
  13. <% response.Write("</tr>") %>
  14. <% rs.movenext %>
  15. <%wend%>
Thanks.
Mar 6 '08 #1
4 1363
markrawlingson
346 Expert 100+
This can happen if there's a problem with the AJAX response, like a 404 or an error within the ASP script you're trying to call.

In the below code

Expand|Select|Wrap|Line Numbers
  1. function stateChanged() {
  2.    if (xmlHttp.readyState==4) {
  3.       document.getElementById("div_id").innerHTML=xmlHttp.responseText;
  4.    }
  5. }
  6.  
You should put in some error handling..
Expand|Select|Wrap|Line Numbers
  1.    function stateChanged() {
  2.       if (xmlHttp.readyState==4) {
  3.          document.getElementById("div_id").innerHTML=xmlHttp.responseText;
  4.       } else if(xmlHttp.readyState == 4 && xmlHttp.status != 200) {
  5.          document.getElementById("div_id").innerHTML= 'ERROR! See Below! <br /><br />' + xmlHttp.responseText;
  6.       }
  7.  
So if the page throws a 404, for instance, the div layer will be filled in with something like page cannot be displayed. If it's a scripting error in the page you're trying to call, the div will be filled in with the vbscript error information. Once you get this, if you still can't figure it out.. write back with the error and we'll take it from there :)
Sincerely,
Mark
Mar 6 '08 #2
DrBunchman
979 Expert 512MB
Another possibility is that the contents of your second page is not displaying because the recordset you create there is not returning any rows.

Have you tried replacing the contents of your secondary page with a simple

<% Response.Write("variable=" & Request.QueryString("variable")) %>

to make sure that your data is being passed correctly and to test whether your AJAX call is working at all?

I'd definitely put in the error trapping as Mark said because, although I've tested your script and it worked correctly for me, different browsers handle AJAX differently.

Let us know how you get on.

Dr B
Mar 7 '08 #3
This can happen if there's a problem with the AJAX response, like a 404 or an error within the ASP script you're trying to call.

In the below code

Expand|Select|Wrap|Line Numbers
  1. function stateChanged() {
  2.    if (xmlHttp.readyState==4) {
  3.       document.getElementById("div_id").innerHTML=xmlHttp.responseText;
  4.    }
  5. }
  6.  
You should put in some error handling..
Expand|Select|Wrap|Line Numbers
  1.    function stateChanged() {
  2.       if (xmlHttp.readyState==4) {
  3.          document.getElementById("div_id").innerHTML=xmlHttp.responseText;
  4.       } else if(xmlHttp.readyState == 4 && xmlHttp.status != 200) {
  5.          document.getElementById("div_id").innerHTML= 'ERROR! See Below! <br /><br />' + xmlHttp.responseText;
  6.       }
  7.  
So if the page throws a 404, for instance, the div layer will be filled in with something like page cannot be displayed. If it's a scripting error in the page you're trying to call, the div will be filled in with the vbscript error information. Once you get this, if you still can't figure it out.. write back with the error and we'll take it from there :)
Sincerely,
Mark
No. I'm not get any errors, I'm not getting anything at all in the div.

Should the listbox read like this?
[code]
"><form id="form3" name="form3" method="get">
<select name="selectlist" size="10" id="list"onfocus="show(<%=rsS("ID")%>)">
<%while not rsS.eof%>
<option id="<%=rsS("ID")%>"><%= rsS("Name") %></option>
<% rsS.movenext
wend%>
</select>
</form>
[code]
Mar 10 '08 #4
Thank you to both of you for your help. I did get it to work and it works beautifully now. I managed to fool with it and found out that I did not have the some things done correctly, like using the value of the listbox option instead of the id for it and I changed the div to a span. Thanks again.
Mar 13 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Yarco | last post: by
4 posts views Thread by bobzimuta | last post: by
8 posts views Thread by needin4mation | last post: by
11 posts views Thread by John Smith | last post: by
10 posts views Thread by Steve | last post: by
reply views Thread by melledge | last post: by
31 posts views Thread by Tony | last post: by
23 posts views Thread by Allan Ebdrup | last post: by
3 posts views Thread by =?Utf-8?B?bWNpbWFnaW5n?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.