PDA

View Full Version : Can Ajax help me


markakapops
06-16-2009, 10:54 AM
I have taken on the task to improve our company store web site. I am trying to create an orderverify page that allows the user to make changes to payment method, ship to address and shipping vendor, via list boxes rather than links that load a new page for each category. This would effect existing customers and the data is pulled using stored procedures.

Server side, the stored procedure runs and we move the data into arrays.
For x = 0 to AddressCount-1
ShippingAddress(x) = rs.fields("ShippingAddress") 'key numeric field
CountryName(x) = trim(rs.fields("CountryName"))
StateAbv(x) = trim(rs.fields("StateAbv"))
StateName(x) = trim(rs.fields("StateName"))
Zip(x) = trim(rs.fields("ZIP"))
County(x) = trim(rs.Fields("County"))
City(x) = trim(rs.fields("City"))
Street2(x) = trim(rs.fields("Street2"))
Street(x) = trim(rs.fields("Street"))
Attention(x) = trim(rs.fields("Attention"))
Name(x) = trim(rs.fields("Name"))
rs.MoveNext
Next

As far as I have gone.

Filling the select list box with the available ship to names
<select id="Ship_fullname" name="Ship_fullname" onchange="getIndex()">
<%
For x = 0 to AddressCount-1
Response.Write("<option value=" & ShippingAddress(x) & "'>" & Name(x) & "</option>")
Next
%>
</select>

I then use the default choice of X(0) to fill in the remaining fields
<div>
<label for="Ship_street_1">address:</label>
<input id="Ship_street_1" type="text" name="Ship_street_1" value=" <%Response.Write(Street(0))%>" />
</div>
So the array is server side, and the any change to the list box would be client side, from the information I have gathered this sounds like a job for ajax.

Any help would be appreciated, even a none ajax solution.
Thank you
Mark

Bill Wilkinson
06-16-2009, 05:00 PM
First of all, instead of getting all those individual arrays, you should learn to use ADODB.Recordset.GetRows( ), which converts the *ENTIRE* recordset into a two dimensional array, for you.

However... Since you are getting ALL the address info for ALL the possible records, I would say that you shouldn't even use GetRows() and clearly you shouldn't use AJAX.

Instead, use ADODB.Recordset.GetString() to convert the recordset into a *JAVASCRIPT ARRAY* (of JS objects, of course). Then use JS coding to construct the dropdown and JS coding to pull the rest of the data from the relevant elements of the array into the <form> fields.

Example code (I'll just show 3 fields instead of all 11):

<html>
<head>
<%
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "...your connection string..."

' yes, of course this could be done via a SP
SQL = "SELECT ShippingAddress, ZIP, Attention "
& " FROM customerShipping WHERE custid = " & custid
Set RS = Conn.Execute( SQL )

prefix = " new CustShip("""
midfix = ""","""
suffix1 = """)"
suffix2 = "," & vbNewLine

jsData = RS.GetString( , , midfix, suffix1 & suffix2 & prefix )
jsData = prefix & Left( jsData, Len(jsData) - Len(suffix2) - Len(prefix) )
RS.Close
conn.Close ' if not used again this page
%>
<script>
// constructor for a CustShip object:
function CustShip( adr, zip, attn )
{
this.address = adr;
this.zipCode = zip;
this.attention = attn;
}
// now the array of all CustShip objects:
allShips = new Array(
<%=jsData%>
);

function getOptions(sel)
{
for ( var o = 0; o < allShips.length; ++o )
{
var ship = allShips[o];
sel.options[sel.options.length] =
new Option( ship.address, ship.address );
}
}

function shipChoice( sel )
{
if ( sel.selectedIndex == 0 ) return;
var form = sel.form;
var o = sel.selectedIndex(-1);
var ship = allShips[o];
form.zipCode.value = ship.zipCode;
form.attention.value = ship.attention;
}
</script>
</head>
<body onload="getOptions(document.TheForm.shippingAddress);">
<form name="TheForm" action="whatever.asp" method=post>
... other form fields ...

Ship to:
<select name="shippingAddress" onchange="shipChoice(sel);">
<option>-- choose one--</option>
</select>
<input name="zipCode" READONLY>
<input name="attention" READONLY>
...
</form>
</body>
</html>


There *is* an FAQ in the ASPFAQs (see link in center above) in the category "Databases, General" that discusses GetString.

Code is utterly untested; use at your own risk. Warranted against only those bugs with 6 or 8 legs for first 30 nanoseconds.