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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP function with AJAX calls

Status
Not open for further replies.
Oct 11, 2006
300
US
Hi,

Can you please let me know what I am doing wrong in the ASP and AJAX part because I do not seem to be calling the AJAX function properly...

What I would like to see is onload of the page, I see only the top level category "Beverage" which is a link. When I click on the "Beverage", then I can see the products that fall under the category "Beverage" using this AJAX function.

I am able to get the products right. But I am not calling the products through AJAX. That is where my trouble lies.

This is something one can try on their machines as well as this is related to the a commonly available database "Northwind"

Thanks.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<%
Dim conn

Set conn = Server.CreateObject("ADODB.Connection")

Conn.open "PROVIDER=SQLOLEDB;DATA SOURCE=ServerName;UID=dbo;PWD=password;DATABASE=Northwind "

'If Conn.State = 0 Then
'  Response.Write "The ADO Connection is closed."
'  Response.End
'End If

'If Conn.State = 1 Then
'  Response.Write "The ADO Connection is open."
'  Response.End
'End If

%>

<head>
<style type="text/css">
* {
   border: 0;
   padding: 0;
   margin: 0;
}

#menu {
  padding:0;
  margin:0;
  }
#menu li {
  list-style-type:none;
  }

#menu ul {
padding: 0;
margin: 6px;
list-style-type: none;
}

a.a_style:link {color:#0000ff; text-decoration:none;}
a.a_style:visited {color:#0000ff; text-decoration:none;}
a.a_style:hover {color:#ff0000; text-decoration:underline;}
a.a_style:hover {color:#ff0000; text-decoration:underline;}

</style>

<script type="text/javascript">

var xmlHttp

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

var url="Categories_dynamic.asp"
url=url+"?ID="+str
alert(url);
//url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        objID = 'UI_'+el;
        document.getElementById(objID).innerHTML=xmlHttp.responseText
        //document.getElementById("content_products").innerHTML=xmlHttp.responseText
    }
}

function GetXmlHttpObject()
{
var objXMLHttp=null
if (window.XMLHttpRequest) //Safari, Mozilla browers
{
    objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject) //IE browsers
{
    objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}

function s_Hide(el){
    alert("Showing Element: "+el);
    objID = 'UI_'+el;
    alert(objID);
    obj = document.getElementById(objID).style;
    (obj.display == 'none')? obj.display = 'block' : obj.display = 'none';
}

</script>

<title>On Demand Building of tree with User OnClick</title>
</head>

<body>

<form name="Customers" id="Customers" action="">
<%
startTime = Now()

'get the Products using a function

Function GetProducts(CatID, Counter)

        sql = "SELECT CategoryID, ProductName, ProductID FROM Products " & _
              "where CategoryID = " & Request.QueryString("ID") & " " & _
              "order by ProductName "

        'Response.Write sql
        'Response.End

        Dim rs2
        set rs2 = Server.CreateObject("ADODB.Recordset")
        rs2.open sql, Conn

        Dim aProducts

        If not rs2.EOF then

            aProducts = rs2.GetRows()

            'Close Recordset to use the new array with the 2 columns data
            rs2.Close()
            set rs2 = Nothing

            'Declare Constants for the above SQL columns for better readability
            'Use these Constants instead of referring to the array numeric indexes
            Const c_CatID = 0
            Const c_ProductName = 1
            Const c_ProductID = 2

            'Ubound(MyArray,1) 'Returns the Number of Columns
            'Ubound(MyArray,2) 'Returns the Number of Rows

            Dim iRowLoop

            For iRowLoop = 0 to UBound(aProducts, 2)
            %>
            <li>
                    <input type="radio" name="prodid" id="prodid<%=aProducts(c_ProductID,iRowLoop)%>" value="<%=aProducts(c_ProductID,iRowLoop)%>">
                    <%=aProducts(c_ProductName,iRowLoop)%>
                    <%
                    Counter = Counter + 1
                    Counter = Counter - 1
                    %>

            </li>
            <%
            Next 'iRowLoop
        End If 'rs2.EOF

End Function
%>
<%

Dim rs
Set rs = Server.CreateObject("ADODB.Recordset")
sSQL = "SELECT CategoryID, CategoryName FROM Categories WHERE CategoryID = 1"

rs.open sSQL, Conn
'Response.Write sSQL

Dim aCategory

If not rs.EOF Then
    'Dump the recordset into the above array
    aCategory = rs.getRows()

    rs.Close
    Set rs = Nothing

    Dim iRows

    For iRows = 0 to UBound(aCategory, 2)
    CatID = aCategory(0, iRows)
    %>
    <ul id="menu">
        <li>
            <input type="radio" checked name="prodid" id="Catid<%=aCategory(0, iRows)%>" value="<%=aCategory(0, iRows)%>">
            <a href="Categories_dynamic.asp" class="a_style" onclick="showProducts(<%=aCategory(0, iRows)%>);s_Hide(this);"><%=aCategory(1, iRows)%></a>
            <ul id="UI_<%=aCategory(0, iRows)%>" style="display:none;">
            <%
            'QS = Request.Querystring("ID")
            'Now pass the querystring to the following function.
            if QS = trim(aCategory(0, iRows)) then
                Call GetProducts (CatID, Counter)
            end if
            %>
            </ul>
        </li>
    </ul>
    <%
    Next 'iRows
End If
%>
</form>
</body>
</html>
<%
Conn.Close
Set Conn = Nothing
%>
 
Are you using this script for the browser to load and for the AJAX script to call? Or do you have a second script for the AJAX to call that doesn't have body, head, script, and form tags, but still returns the sub-items?

 
Hi,

Thanks for the response. The above code is one single ASP page. It is the entire code. Is It too much to ask you to try it your machine and see how it works now?

I first use the server to load only the top level category - in this case 1 which is "Beverage". I use AJAX function showProducts(str) to show the products. So the function showProducts(str) shows a URL which is this page itself. Is that possible? Or do I have to split into 2 ASP pages?

Since I could not get the above approach working, I got it working by splitting into 2 pages.

1. Categories_dynamic.asp: which shows only the top link - Beverage.

2. getProducts.asp: When I click on this "Beverage" link, the function makes a call to getProducts.asp?ID=1

So getProducts(CatID, Counter) takes the querystring and shows the products.

This works in this case. But in my case, the categories are n level deep. So with getProducts.asp which is only ASP code, how can I show a Javascript function where is no javascript code in the <HEAD> tag?

That is why I wanted to work with only 1 page rather than splitting with 2 pages.

Did I explain it good enough?

Thanks.
 
Even if I were to split the code, how my getProducts.asp call a Javascript function without having a script tag in the asp page as it is an embedded within the Categories_dynamic.asp

Wait a minute...as I am writing this message I noticed that when I view the page source, I cannot see the Products listing in the HTML view source. Just the div "Products_Content", but no products listed in the view source, though I can see it in the browser. Why do I find that strange?

Thanks.
 
Why do I find that strange?
Well it's perfectly normal.

Content created by javascript isn't in the source code, only content sent from the server would appear when viewing the source.

Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
I am still not able to get this working in a simple page. Please help me. I am badly stuck.... :(

I am trying to view the contents of the Beverage Category. I have built it like a hierarchy in the form of styled <UL> tags.

Please assist me in combining the 2 pages into one page. I would extremely grateful.

My code is:

Categories_Dynamic.asp
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<!--#INCLUDE Virtual="/Scripts/Padmaja/Testing2/DB_Northwind.asp"-->
<head>
<style type="text/css">
* {
   border: 0;
   padding: 0;
   margin: 0;
}

#menu {
  padding:0;
  margin:0;
  }
#menu li {
  list-style-type:none;
  }

#menu ul {
padding: 0;
margin: 6px;
list-style-type: none;
}

a.a_style:link {color:#0000ff; text-decoration:none;}
a.a_style:visited {color:#0000ff; text-decoration:none;}
a.a_style:hover {color:#ff0000; text-decoration:underline;}
a.a_style:hover {color:#ff0000; text-decoration:underline;}

</style>

<script type="text/javascript">

var xmlHttp

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

var url="getProducts.asp"
url=url+"?ID="+str
//alert(url);
//url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged1
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged1()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		//objID = 'UI_'+el;
		//document.getElementById(objID).innerHTML=xmlHttp.responseText
		document.getElementById("content_products").innerHTML=xmlHttp.responseText
	}
}

function GetXmlHttpObject()
{
var objXMLHttp=null
if (window.XMLHttpRequest) //Safari, Mozilla browers
{
	objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject) //IE browsers
{
	objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}

function s_Hide(el){
	//alert("Showing Element: "+el);
	objID = 'UI_'+el;
	//alert(objID);
	obj = document.getElementById(objID).style;
	(obj.display == 'none')? obj.display = 'block' : obj.display = 'none';
}

</script>

<title>On Demand Building of tree with User OnClick</title>
</head>

<body>

<form name="Customers" id="Customers" action="">

<%

Dim rs
Set rs = Server.CreateObject("ADODB.Recordset")
sSQL = "SELECT CategoryID, CategoryName FROM Categories WHERE CategoryID = 2"

rs.open sSQL, Conn

Dim aCategory

If not rs.EOF Then
    'Dump the recordset into the above array
    aCategory = rs.getRows()

    rs.Close
    Set rs = Nothing

    Dim iRows

    For iRows = 0 to UBound(aCategory, 2)
    CatID = aCategory(0, iRows)
    %>
    <ul id="menu">
        <li>
            <input type="radio" checked name="prodid" id="Catid<%=aCategory(0, iRows)%>" value="<%=aCategory(0, iRows)%>">
            <a href="#" class="a_style" onclick="showProducts(<%=aCategory(0, iRows)%>);s_Hide('<%=aCategory(0, iRows)%>'); return false;"><%=aCategory(1, iRows)%></a>
            <ul id="UI_<%=aCategory(0, iRows)%>">
            <%
            'QS = Request.Querystring("ID")
            'Now pass the querystring to the following function.
            'if QS = trim(aCategory(0, iRows)) then
                'Call GetProducts (CatID, Counter)
            'end if
            %>
            <div id="content_products">
            </div>
            </ul>
        </li>
    </ul>
    <%
    Next 'iRows
End If
%>
</form>
</body>
</html>
<%
Conn.Close
Set Conn = Nothing
%>

getProducts.asp
Code:
<!--#INCLUDE Virtual="/Scripts/Padmaja/Testing2/DB_NOrthwind.asp"-->
<%
ProdID = Request.Querystring("ID")
'Response.Write ProdID
Call GetProducts (ProdID, Counter)


startTime = Now()

'get the Products using a function

Function GetProducts(ProdID, Counter)

		sql = "SELECT CategoryID, ProductName, ProductID FROM Products " & _
		      "where CategoryID = " & ProdID & " " & _
		      "order by ProductName "

		'Response.Write sql
		'Response.End

		Dim rs2
		set rs2 = Server.CreateObject("ADODB.Recordset")
		rs2.open sql, Conn

		Dim aProducts

		If not rs2.EOF then

			aProducts = rs2.GetRows()

			'Close Recordset to use the new array with the 2 columns data
			rs2.Close()
			set rs2 = Nothing

			'Declare Constants for the above SQL columns for better readability
			'Use these Constants instead of referring to the array numeric indexes
			Const c_CatID = 0
			Const c_ProductName = 1
			Const c_ProductID = 2

			'Ubound(MyArray,1) 'Returns the Number of Columns
			'Ubound(MyArray,2) 'Returns the Number of Rows

			Dim iRowLoop

			For iRowLoop = 0 to UBound(aProducts, 2)
			%>
			<li>
					<input type="radio" name="prodid" id="prodid<%=aProducts(c_ProductID,iRowLoop)%>" value="<%=aProducts(c_ProductID,iRowLoop)%>">
					<%=aProducts(c_ProductName,iRowLoop)%>
					<%
					Counter = Counter + 1
					Counter = Counter - 1
					%>
			</li>
			<%
			Next 'iRowLoop
		End If 'rs2.EOF

End Function
%>
 
Anybody has answers on how to make this simple thing work which I have no clue...

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top