...

How to convert javascript array to coldfusion array?

korssane
08-05-2011, 09:23 PM
Hi Guys ,


How to get the contents of a javscript array into acoldfusion array? I am coding in Coldfusion MX7.0 and Javascript. . I have a javascript array containing some values. Now i want to insert the values present in this javascript array into the database through an Insert query (within a <cfquery> tag).


thank you

Gjslick
08-06-2011, 06:51 PM
How are you trying to submit the JavaScript data from your webpage to your ColdFusion server? Are you using a standard form submission, or an AJAX request?

korssane
08-08-2011, 02:27 PM
Hi Gjslick,

i am trying to capture them in a form and try to submit them to the databse.

Do you have any other suggestions ?

thanks

Gjslick
08-08-2011, 08:32 PM
Ok, what does your code look like so far? You said you have the array in a JavaScript variable?

korssane
08-08-2011, 08:52 PM
Hi Gjslick,
Thanks for the quick reply

i have been to capture the values i want. Iam capturing then into the same frm field like this :

formfield1 = value1,value2,value3....

I need to insert those values into a databse now ..Can you help with that..
I need to call the formfield1 content as VALUES in my sql script..
something likethat :


insert into TABLE (tablefield1,tablefield2,tablefield2) VALUES formfield1 ;

is it possible ? if not what do you suggest ?

thanks a lot

Gjslick
08-08-2011, 11:38 PM
Ok, cool, I'm glad you got that far.

You have your values in what ColdFusion calls a "list" (values separated by commas, or some other delimiter). You can use the built in list functionality for this. It just depends if your values are numbers, or strings (varchar).

If your values are numbers, you can just directly insert your comma-delimited list directly into your SQL, and they will form a perfectly valid INSERT statement:

<cfquery datasource="mydsn">
INSERT INTO myTable ( field1, field2, field3 )
VALUES ( #form.formfield1# ) <!--- assuming list is like: 1,2,3 --->
</cfquery>


If your values are strings (varchar), then they need quotes for the database. You should be able to do something like this:

<cfquery datasource="mydsn">
INSERT INTO myTable ( field1, field2, field3 )
VALUES (
<cfset numListItems = listLen( form.formfield1 )>
<cfloop from="1" to="#numListItems# index="i">
'#listGetAt( form.formfield1, i )#'<cfif i lt numListItems>,</cfif>
</cfloop>
)
</cfquery>


Try that and let me know how it goes.

korssane
08-12-2011, 06:19 AM
Gjslick,

you're the best. Thank you . You saved my life for this small code. i had the logic in mind but was missing the syntax.

Other question for myou...
can i do the same with excel sata into databse..
let says i have 5 columns by 36 rows fixe that will not change and i wantthe user to paste into a textarea form or a grid and store it into a databse...?

is it doable .. or can you please guide me on that ?

thanks

Gjslick
08-12-2011, 08:30 PM
It would be very hard to determine the format of the data having a user just copy and paste things from an excel file into a textarea. Try it out yourself and see how it looks, lol.

There is a way where you can upload excel files and process them though. Look up how to upload files, and then you can possibly use a Java library called "POI" to read the spreadsheet. Using that is pretty advanced though, so I don't know if you want to get involved. Some people have written ColdFusion wrappers for the POI library to make it a bit simpler however. Here's an example: http://www.bennadel.com/projects/poi-utility.htm, and a bit on how to use it: http://www.bennadel.com/blog/474-ColdFusion-Component-Wrapper-For-POI-To-Read-And-Write-Excel-Files.htm

korssane
08-13-2011, 08:00 PM
Thanks Gjslick,

i ll try to go through it and see.

Thank you again for your help



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum