Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jun 2011
    Posts
    148
    Thanks
    18
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    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?

  • #3
    Regular Coder
    Join Date
    Jun 2011
    Posts
    148
    Thanks
    18
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    Ok, what does your code look like so far? You said you have the array in a JavaScript variable?

  • #5
    Regular Coder
    Join Date
    Jun 2011
    Posts
    148
    Thanks
    18
    Thanked 0 Times in 0 Posts
    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 10:21 PM.

  • #6
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Jun 2011
    Posts
    148
    Thanks
    18
    Thanked 0 Times in 0 Posts

    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

  • #8
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    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

  • #9
    Regular Coder
    Join Date
    Jun 2011
    Posts
    148
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Thanks Gjslick,

    i ll try to go through it and see.

    Thank you again for your help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •