p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > Web Programming > JavaScript > Ajax
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Ajax the combination of XHTML, CSS, DOM, XML, XSLT, XMLHttpRequest, and JavaScript

Welcome to the p2p.wrox.com Forums.

You are currently viewing the Ajax section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 5th, 2009, 12:31 PM
Registered User
Points: 24, Level: 1
Points: 24, Level: 1 Points: 24, Level: 1 Points: 24, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Trying to get this to play nice

Hello everyone,
I have a problem that is about to drive me nuts. I am trying to get asp and ajax to work. I am using mySql database(4.0), IIS 6.0, and javascript code. I also have a working version of this on a PhP site. I can get things to work in that setup, but I can not get the results to display in the ASP version.
Here is the code I am using currently
Code:
<%@ LANGUAGE = JavaScript%>
<%
 var myProdID = Request.QueryString("prod");
 var strConn = "DRIVER={MySQL ODBC 3.51 
Driver};SERVER=........;DATABASE=.......;UID=......;PWD=......";
 var oConn = Server.CreateObject("ADODB.Connection");
 oConn.Open(strConn);
 var oRs;
 oRs = oConn.Execute("SELECT prodID, prodName FROM tblProds WHERE prodID = 'myProdID'");
 var details;
 details = oRs("prodName").Value;
 
     While Not oRs.EOF
      Response.Write details;
      oRs.MoveNext();
     Wend
 
%>
I have received several different errors from running this, the most recent being this

showStatus() called with ready state of 4 and a response text of "JScript compilation error 800a03ec expected ;" and it has the error arrow (^) pointing to right before the While Not .. line of code. I have checked everything I know to do and I don't see anywhere where I left out a semicolon. This is just a simple experiment to see if I can get things to work with this setup.
Does anyone have any idea why this is not working? I would really appreciate some insight.
Thanks in advance
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old July 6th, 2009, 04:00 AM
joefawcett's Avatar
Wrox Author
Points: 8,994, Level: 40
Points: 8,994, Level: 40 Points: 8,994, Level: 40 Points: 8,994, Level: 40
Activity: 11%
Activity: 11% Activity: 11% Activity: 11%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 2,922
Thanks: 0
Thanked 13 Times in 12 Posts
Default

I can't see any Ajax there. Does the page you showed work and display the records from the query? I'd be surprised if it worked as you're not using the myProdID variable, and you're leaving yourself exposed to a SQL injection attack. So this:
Code:
oRs = oConn.Execute("SELECT prodID, prodName FROM tblProds WHERE prodID = 'myProdID'");
should be:
Code:
oRs = oConn.Execute("SELECT prodID, prodName FROM tblProds WHERE prodID = '" + myProdID + "'");
but you should be using parameterised query.
__________________
--

Joe
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old July 6th, 2009, 11:47 AM
Registered User
Points: 24, Level: 1
Points: 24, Level: 1 Points: 24, Level: 1 Points: 24, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thanks for the insight

Hello Joe,
Thank you for your reply. I agree with the sql injection vulnerability statement. I am just trying to see if I can get this to work before I do any advanced work or even make this available to the public.
That code you saw was what I had placed on the server to respond to the form page I have included below.
That is where the ajax part is.
Code:
<!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>ASP AJAX Page</title>
 </head>
 <body>
  <form id="nextTry">
   <label for="prodID">Product ID: </label><input type="text" name="prodID" id="prodID" onChange="findProdName();" />
   <label for="prodName">Product Name: </label><input type="text" name="prodName" id="prodName" />
  </form>
  <script language="javascript" type="text/javascript" >
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
var request = getHTTPObject(); // We create the HTTP Object
 

   function findProdName()
   {
    var prodctID = document.getElementById("prodID").value;
    if((prodctID == null) || (prodctID == "")) return;
    var chkProd = "ajaxProdsJS.asp?prod=" + escape(prodctID);
    request.open("GET", chkProd, true);
    request.onreadystatechange = showStatus;
    request.send(null);
   }
   function showProdName()
   {
    if (request.readydstate==4)
    {
     var prodNm = request.responseText;
     document.getElementById("prodName").value = prodNm;
    }
   }
   function showStatus()
   {
    alert("showStatus() called with ready state of" + request.readystate + " and a response text of " + request.responseText );
   }
  </script>
 </body>
</html
I used the showStatus function to display and got the ready state of 4, but it said there was a semicolon expected on line 17 of the code I posted earlier.
But thanks for what you posted. I think I see what I did wrong and I will try that next. I am used to writing server side in vbscript and the migration to javascript messed me up. I am about ready to give up on this in asp and just stick with the PhP version, but I will try this and see what it does.
Again, thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old July 6th, 2009, 03:28 PM
Registered User
Points: 24, Level: 1
Points: 24, Level: 1 Points: 24, Level: 1 Points: 24, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello again Joe,
I took your suggestion and changed my code to reflect that improvement and got the same alert message (ie ready state 4 and expectected semicolon on line 17). But thanks anyway
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old July 7th, 2009, 04:18 AM
joefawcett's Avatar
Wrox Author
Points: 8,994, Level: 40
Points: 8,994, Level: 40 Points: 8,994, Level: 40 Points: 8,994, Level: 40
Activity: 11%
Activity: 11% Activity: 11% Activity: 11%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 2,922
Thanks: 0
Thanked 13 Times in 12 Posts
Default

Well debugging is done one step at atime. Is the asp page working in stand alone mode? Does it show the product details given a particular ID?
__________________
--

Joe
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old July 8th, 2009, 12:17 PM
Registered User
Points: 24, Level: 1
Points: 24, Level: 1 Points: 24, Level: 1 Points: 24, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thanks for your help

Joe,
Thanks again for your interest in my problem. Yes, when using a straight ASP call to mySQL the page has no problem displaying the requested information
Here is an example
Code:
<div id="featureProducts">
    <div id="featureProdHeader">
     <p>Featured Items</p>
    </div>
    <div id="featureProd1">
     <%
     Dim strConn, oConn, qry, oRs, strImg
     strConn = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=......;"
     strConn = strConn & "DATABASE=........;UID=..........;PWD=.........."
     Set oConn = Server.CreateObject("ADODB.Connection")
     oConn.Open strConn
     qry = "SELECT prodID, prodName, prodSmImg FROM tblProds ORDER BY RAND() LIMIT 1"
     Set oRs = oConn.Execute(qry)
     Response.write "<p>" & oRs("prodID") & "<br />"
     Response.write oRs("prodName") & "<br />"
     strImg = "<img src='images/sm/"
     strImg = strImg & oRs("prodSmImg") & "' />"
     strImg = strImg & "</p>"
     Response.write strImg
     oRs.close
     oConn.close
     Set oRs = nothing
     Set oConn = nothing
     %>
I actually have that same code running to pull the images for feature product 2 and feature product 3 so that it will display random products each time the page is reloaded. It is designed to be the front page of an e-comm store. I had no problem getting that to work. I also have a products page and a prod_desc page that work fine. In fact, on the prods.asp page I have 2 different div sections calling the mySQL database based on which link you click to view product types Here is that code

Code:
<div id="level2LeftNav">
     <%
     Dim strConn2, oConn2, qry2, oRs2
     strConn2 = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=.........;"
     strConn2 = strConn2 & "DATABASE=..........;PWD=............."
     Set oConn2 = Server.CreateObject("ADODB.Connection")
     oConn2.Open strConn2
     qry2 = "SELECT DISTINCT prodSubCat FROM tblProds WHERE prodCat = '" & catName & "'"
     Set oRs2 = oConn2.Execute(qry2)
     if not oRs2.EOF then
     while not oRs2.EOF
     Response.write "<ul><li>" & oRs2("prodSubCat") & "</li>"
     oRs2.movenext
     wend
     oRs2.close
     end if
     Response.write "</ul>"
     oConn2.close
     Set oRs2 = nothing
     Set oConn2 = nothing
     %>
    </div>
    <div id="level2ProdsSection">
     <%
     Dim strConn, oConn, qry, oRs, strImg
     strConn = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=..........;"
     strConn = strConn & "DATABASE=.......;UID=..........;PWD=.........."
     Set oConn = Server.CreateObject("ADODB.Connection")
     oConn.Open strConn
     qry = "SELECT prodID, prodName, prodPrice, prodSmImg FROM tblProds WHERE prodCat = '" & catName & "'"
     Set oRs = oConn.Execute(qry)
     if not oRs.EOF then
     while not oRs.EOF
     Response.write "<p>" & oRs("prodID") & "<br />"
     Response.write oRs("prodName") & "<br />"
     strImg = "<img src='images/sm/"
     strImg = strImg & oRs("prodSmImg") & "' />"
     strImg = strImg & "<br />"
     Response.write strImg
     Response.write oRs("prodPrice") & "</p>"
     oRs.movenext
     wend
     oRs.close
     end if
     oConn.close
     Set oRs = nothing
     Set oConn = nothing
     %>
Again, this works fine and as expected. It is just seems to be something that doesn't work when I try to make the call to mySQL from XMLHttpRequest.
Once again, thank you for your interest in my problem. I really appreciate your help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #7 (permalink)  
Old July 8th, 2009, 12:49 PM
Registered User
Points: 24, Level: 1
Points: 24, Level: 1 Points: 24, Level: 1 Points: 24, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello again Joe,
I wanted to include the complete revised version of the Server side script that the AJAX page was calling to see if you could see something I was missing there. Thanks again
Code:
<%@ LANGUAGE = JavaScript%>
<%
 var myProdID = Request.QueryString("prod");
 var strConn = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=................;DATABASE=...............;UID=...............;PWD=..............";
 var oConn = Server.CreateObject("ADODB.Connection");
 oConn.Open(strConn);
 var oRs;
 oRs = oConn.Execute("SELECT prodID, prodName FROM tblProds WHERE prodID = '" + myProdID + "'");
 var details;
 details = oRs("prodName").Value;
 Response.Write details
 
%>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
This would be nice... alliancejhall P2P and Wrox.com Feedback 1 December 19th, 2008 11:02 AM
Nice Article on Generic Paging jimibt BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 August 15th, 2007 11:49 AM
Nice book..but ShaneTheMaster BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 5 April 13th, 2007 11:44 AM
Nice Textbook!! juhu Pro VB 6 2 May 3rd, 2005 07:24 PM
nice intro scree tsc Access VBA 11 September 29th, 2003 02:11 PM



All times are GMT -4. The time now is 11:30 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc