Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > ColdFusion

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-05-2011, 09:23 PM   PM User | #1
korssane
Regular Coder

 
Join Date: Jun 2011
Posts: 148
Thanks: 18
Thanked 0 Times in 0 Posts
korssane is an unknown quantity at this point
How to convert javascript array to coldfusion array?

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
korssane is offline   Reply With Quote
Old 08-06-2011, 06:51 PM   PM User | #2
Gjslick
Regular Coder

 
Join Date: Feb 2009
Location: NJ, USA
Posts: 476
Thanks: 2
Thanked 70 Times in 69 Posts
Gjslick will become famous soon enough
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?
Gjslick is offline   Reply With Quote
Old 08-08-2011, 02:27 PM   PM User | #3
korssane
Regular Coder

 
Join Date: Jun 2011
Posts: 148
Thanks: 18
Thanked 0 Times in 0 Posts
korssane is an unknown quantity at this point
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
korssane is offline   Reply With Quote
Old 08-08-2011, 08:32 PM   PM User | #4
Gjslick
Regular Coder

 
Join Date: Feb 2009
Location: NJ, USA
Posts: 476
Thanks: 2
Thanked 70 Times in 69 Posts
Gjslick will become famous soon enough
Ok, what does your code look like so far? You said you have the array in a JavaScript variable?
Gjslick is offline   Reply With Quote
Old 08-08-2011, 08:52 PM   PM User | #5
korssane
Regular Coder

 
Join Date: Jun 2011
Posts: 148
Thanks: 18
Thanked 0 Times in 0 Posts
korssane is an unknown quantity at this point
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

Last edited by korssane; 08-08-2011 at 09:21 PM..
korssane is offline   Reply With Quote
Old 08-08-2011, 11:38 PM   PM User | #6
Gjslick
Regular Coder

 
Join Date: Feb 2009
Location: NJ, USA
Posts: 476
Thanks: 2
Thanked 70 Times in 69 Posts
Gjslick will become famous soon enough
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:
Code:
<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:
Code:
<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.
Gjslick is offline   Reply With Quote
Old 08-12-2011, 06:19 AM   PM User | #7
korssane
Regular Coder

 
Join Date: Jun 2011
Posts: 148
Thanks: 18
Thanked 0 Times in 0 Posts
korssane is an unknown quantity at this point
Thumbs up

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
korssane is offline   Reply With Quote
Old 08-12-2011, 08:30 PM   PM User | #8
Gjslick
Regular Coder

 
Join Date: Feb 2009
Location: NJ, USA
Posts: 476
Thanks: 2
Thanked 70 Times in 69 Posts
Gjslick will become famous soon enough
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-Col...xcel-Files.htm
Gjslick is offline   Reply With Quote
Old 08-13-2011, 08:00 PM   PM User | #9
korssane
Regular Coder

 
Join Date: Jun 2011
Posts: 148
Thanks: 18
Thanked 0 Times in 0 Posts
korssane is an unknown quantity at this point
Thanks Gjslick,

i ll try to go through it and see.

Thank you again for your help
korssane is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:29 PM.


Advertisement
Log in to turn off these ads.