Skip to main content
Inspiring
July 4, 2008
Question

CF Array and JavaScript

  • July 4, 2008
  • 21 replies
  • 2408 views
Is it possible to manipulate arrays created in coldfusion with JavaScript?
All I'm trying to do is update a count (array length) dynamically everytime something is added to the array. So I got the brilliant idea to add the data submitted using JS and do a dynamic count update at the same time. But what I get is the JS error saying the array is undefined.

Suggestions?
This topic has been closed for replies.

21 replies

Inspiring
July 8, 2008
Dinghus wrote:
>
> Maybe I can manipulate cookies in the same way, tho I'm thinking sandbox rules
> probably will prevent that.
>

I've used JavaScript to create and manipulate cookies in the past.

DinghusAuthor
Inspiring
July 8, 2008
Let me clairfy something to everyone who is thinking my idea was obviously wrong. It wasn't.

Javascript can manipulate forms and form data. So if I build a form in ColdFusion it can send data to JavaScript and JS can actually alter that data and send it back. And what is a form? A STRUCTURE. This led me to wonder if it was possible to actually interact with other structures in the some way.

The only mistake was forgetting that forms are part of the DOM where as other structures are not.

Obviously JS and CF can not communicate in real time but assuming they can not communicate is incorrect.

Maybe I can manipulate cookies in the same way, tho I'm thinking sandbox rules probably will prevent that.
Inspiring
July 8, 2008
Dinghus wrote:
> I'm thinking now that I have to do it all by JS
> and then pass the data to a CF page via http request or some such.
>

Exactly! The ColdFusion server does not see the client and the client
does not see the ColdFusion server.

The data must pass back and forth with http requests and responses.
Unless one is working with Flex or similar techniques that open a
connection between the client and server outside of the HTTP standard.

You can use AJAX functionality if you would like to hide these requests
and responses behind the scenes so the user does not see page refreshes.

DinghusAuthor
Inspiring
July 8, 2008
Check boxes would be the same. I need the data in the array for other reasons. Almost like a shopping cart. I'm thinking now that I have to do it all by JS and then pass the data to a CF page via http request or some such.
Inspiring
July 8, 2008
Check boxes seem so much simpler than what you are attempting.
DinghusAuthor
Inspiring
July 8, 2008
Hmmm first, JS does have 2D arrays, just a bit of playing around to do it. BUT I'm not trying to do that so it is a moot point.

Basically I have a long list of people and the user will be selecting them. This will add them to an array. There is a display of people selected at the top. They can also be removed.

So I want the person's ID added to the array and the count updated when a button is clicked next to their name. All without refreshing the page.

I do an onClick to call a JS function that can update the count but I want it to insert the ID into an array at the same time. Then when the user clicks "DONE" the array is sent to the database.

I see I will probably have to come up with another way of doing the temp storage tho. Maybe a hidden form field.
BKBK
Community Expert
Community Expert
July 7, 2008
Is it possible to manipulate arrays created in coldfusion with JavaScript?
All I'm trying to do is update a count (array length) dynamically everytime something is added to the array. So I got the brilliant idea to add the data submitted using JS and do a dynamic count update at the same time. But what I get is the JS error saying the array is undefined.

Suggestions?


There could be ways to do that, using the new Javascript capabilities of Coldfusion 8. Let's see your code.

Inspiring
July 7, 2008
Dinghus wrote:
> What I'm trying to do is insert data into an array, similar to a shopping cart,
> and then update a count of items in the array. I want to stay on the same page
> instead of refreshing it. The count will just be a dynamic number. The data in
> the array will be sent to the database at the end of the session.
>

Are you keeping it straight that ColdFusion runs on the server and
JavaScript runs on the client and never shall the two meet!

I.E. Unless you have taken steps to have a JavaScript COPY of the
ColdFusion array delivered to the client, it does not exist there.

Inspiring
July 7, 2008
You can use the toScript function to convert a 1D cf array to js. js does not have 2D arrays.

Regarding this, "The data in the array will be sent to the database at the end of the session. ", either you didn't say what you meant or you have an upcoming problem.
DinghusAuthor
Inspiring
July 7, 2008
What I'm trying to do is insert data into an array, similar to a shopping cart, and then update a count of items in the array. I want to stay on the same page instead of refreshing it. The count will just be a dynamic number. The data in the array will be sent to the database at the end of the session.