...

View Full Version : JavaScript/Cold Fusion SQL Data xfer



trsands
01-06-2004, 01:16 PM
I have a Java script which populates an array . What I want to do is when I submit the page the array be available to cold fusion so that I can update my SQL 2K database via an existing cold fusion datasource. Or is there a cleaner way to get data from JS to SQL? HeLLLppp!!!!

P.S. Does anyone know how to make Dreamweaver MX create a new window when testing scripts rather than overwriting the last used window?

Danne
01-06-2004, 01:42 PM
Generate some hidden inputs from your array before submitting the form:



var f=document.forms["myForm"];
for (var i=0; i<arr.length; i++) {
var hidden=document.createElement("INPUT");
hidden.type="hidden";
hidden.name="myValues";
hidden.value=arr[ i ];
f.appendChild(hidden);
}

glenngv
01-06-2004, 01:49 PM
or use just one hidden field that contains the comma-delimited values of the array

document.myformname.myhiddenfield.value = myArray.join();

and then retrieve and split it up in cold fusion

Danne
01-06-2004, 01:59 PM
Originally posted by glenngv
or use just one hidden field that contains the comma-delimited values of the array

document.myformname.myhiddenfield.value = myArray.join();

and then retrieve and split it up in cold fusion

As long as you don't have commas in the values, why make things too complicated..:D

glenngv
01-06-2004, 02:05 PM
You can always use a different delimiter that will not be in the values.

myArray.join("|");
myArray.join("~!@$");

trsands
01-06-2004, 07:27 PM
Thanks guys. now just to complicate it a bit further. The array is made up of objects containing purchase data e.g.

object.date
object.time
object.customerid
object.itemcode
object.price
object.quantity

this makes getting the data back on the CF side extremely difficult if I just use a join() right?

Choopernickel
01-06-2004, 07:40 PM
If you're using CF 4 or higher, there are WDDX libraries which will automate the transfer of data between ColdFusion and JavaScript. Check your documentation for more on the <cfwddx ...> tag.

FastCougar
01-06-2004, 08:07 PM
If you are using ColdFusion, why not use ColdFusion's Array function instead of trying to do this in Javascript???

trsands
01-11-2004, 09:15 AM
Cant use CF for array beacuse data is creates as user enters item number for purchase by "onClick"

glenngv
01-12-2004, 05:45 AM
So combine the solution of Danne and mine.



var f=document.forms["myForm"];
for (var i=0; i<arr.length; i++) {
var hidden=document.createElement("INPUT");
hidden.type="hidden";
hidden.name="myValues";
hidden.value=arr[ i ].date + "," + arr[ i ].time + "," + arr[ i ].customerid + "," + arr[ i ].itemcode + "," + arr[ i ].price + "," + arr[ i ].quantity;
f.appendChild(hidden);
}


So you have an array of hidden fields, each containing the comma-delimited purchased data.

trsands
01-12-2004, 05:06 PM
Thanks. the wddx solutions appears to be a 'clean' solution.

Glenn: What wouls be the exact code on the other side to retrieve the data? How will I know how many items are coming across? Its Monday morning and Im a but fuzzy so I just dont get it!

glenngv
01-13-2004, 01:30 AM
I don't know CF but as any other server-side languages, if the fields have the same name, you retrieve it as an array. Then you split each item to form another array.

Choopernickel
01-13-2004, 01:40 PM
In ColdFusion, fields with the same name are submitted as a comma-delimited list. You'll be just fine if you have no commas in the values in the list; that's what you'll have to watch out for, and that's why WDDX may be the best solution.

CF has list operation functions and tags, such as ListLen(aList), ListGetAt(aList, pos), and <cfloop list="#aList#" index="item"></cfloop>, which make this kind of thing handy; however, you really have to protect against commas in your value strings.

glenngv
01-14-2004, 01:22 AM
var f=document.forms["myForm"];
for (var i=0; i<arr.length; i++) {
var hidden=document.createElement("INPUT");
hidden.type="hidden";
hidden.name="myValues";
hidden.value=arr[ i ].date + "|" + arr[ i ].time + "|" + arr[ i ].customerid + "|" + arr[ i ].itemcode + "|" + arr[ i ].price + "|" + arr[ i ].quantity;
f.appendChild(hidden);
}


I change the delimiter from comma to pipe in each of the hidden field's value. As you can see in the type of data (date, time, customerid, itemcode, price, quantity), I don't think that there could be pipe in them. So I think that would be fine. A sample output in the server would be:

01/14/2004|09:17 AM|123456|ABC123|1000.50|2,01/14/2004|09:17 AM|123456|ABC123|1000.50|2,01/14/2004|09:17 AM|123456|ABC123|1000.50|2

But as Choopernickel said, WDDX may be the best solution.

Danne
01-14-2004, 12:47 PM
To continue on glenn's solution, you can transform all pipes and commas in the values to unicode:



function filter(str) {
return new String(str).replace(
/\,/g, escape(",")).replace(
/\|/g, escape("|"));
}

var f=document.forms["myForm"];
for (var i=0; i<arr.length; i++) {
var hidden=document.createElement("INPUT");
hidden.type="hidden";
hidden.name="myValues";
hidden.value=
filter(arr[ i ].date) + "|"
+ filter(arr[ i ].time) + "|"
+ filter(arr[ i ].customerid) + "|"
+ filter(arr[ i ].itemcode) + "|"
+ filter(arr[ i ].price) + "|"
+ filter(arr[ i ].quantity);
f.appendChild(hidden);
}



If there is a simple way to bring unicode values back, using only escape instead of filter is enough. This would change other characters into unicode as well.

glenngv
01-15-2004, 09:33 AM
Aren't the values already escaped when the page gets submitted because they are stored in the (hidden) fields (as opposed to URL)? I think the browser does the escaping automatically.

Danne
01-15-2004, 12:51 PM
Yes, but it's another way to allow pipes and commas in the values.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum