Forum Moderators: open

Message Too Old, No Replies

Scope Question

         

kmbarz

4:11 pm on Aug 25, 2004 (gmt 0)

10+ Year Member



I have a script on a page that fills global array with values from a form. The idea was that I would fill this array and then grab those values when I'm ready to write to my database using coldfusion. As I'm presently drawing a blank, does that "global" javascript array exist as part of my page (and thus available to my CF tags), or only within the context of the script.
Thanks

Bernard Marx

4:21 pm on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it's global, it's global.

>> and thus available to my CF tags

Hmm. Give me a brief overview of ColdFusion, and I might be able to help you!

The array isn't part of the document, if you like. It's actually a property of the window (global object). I don't know if that helps at all...?

>> the context of the script

There isn't really such a thing (#1). Once all scripts have loaded & executed, then it all becomes part of the Javascript object model (loosely).

#1 The one difference is that, if you execute a function call as the page loads, you can only call a function in a statement 'below' the function's definition if it is within the same script block/file. There is some worry about scripts arriving in the wrong order. If the whole scripting caboodle is only initiated onload, then none of this is an issue.

kmbarz

4:44 pm on Aug 25, 2004 (gmt 0)

10+ Year Member



Here's a test I'm trying out:

<script language="javascript">
var myArray = new Array();
myArray[0] = "test";
</script>

To pull a variable into coldfusion, I would usually do something like:

<cfset myVar = myArray[0]> <!--doesn't work-->

So, I know I need something more than myArray[0] here. Cfset does run on the server will pull things in from forms I create, so I think I just need the right way to reference myArray[0]. thanks

kmbarz

4:46 pm on Aug 25, 2004 (gmt 0)

10+ Year Member



ps. and it's not onload. The array is populated dynamically as the user adds records from a form.

Bernard Marx

4:53 pm on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does the tag, <cfset ..> actually appear on the page, like that?
ie. Does it appear in the source code that's sent to the client, or does it only appear in the code on the server - much like a processing instruction for PHP : <? ?> that gets turned into something else?

If so, what does it actually turn into?

kmbarz

5:00 pm on Aug 25, 2004 (gmt 0)

10+ Year Member



It actually appears on the page. When "submit" is clicked, the page goes to the server which knows how to parse the cf tags.

Bernard Marx

5:11 pm on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK. Now, feel free to give up when you get bored of my ignorance of CF (I've been searching around trying to get a handle on it).

Can I question this?:

>> the page goes to the server which knows how to parse the cf tags.

When a form is submitted, it can send information contained in its input fields etc. The whole page doesn't get sent back to the server. I can imagine a scenario where a browser could choose to send information in CFML tags as well, but that scenario requires the browser to understand CFML . I'm sure most browsers don't.

The CFML gets parsed by the server, but before the document is transmitted, surely?

#1 or the document is scripted to read the CFML tags, then create hidden form inputs etc. and that doesn't sound right for something as serious as CF.

kmbarz

5:28 pm on Aug 25, 2004 (gmt 0)

10+ Year Member



My understanding is that my page (say page1.cfm) sits on the webserver with CFserver running on it. The user requests page1.cfm, the server runs any cf tags and then renders up html and in some cases javascript.

So, if page1.cfm was:

<html>...

<cfset myVar = "Hi">
<cfoutput>#myVar#</cfoutput>
...
</html>

the user would see
<html>...
Hi
...
</html>

I have a JS function that will add variables to an array, myArray[0],[1],... In this case, page1.cfm will have a form with an action directing it to page1action.cfm. So I'll have the js array on page1.cfm, I would like page1action.cfm to see and process those variables.

Bernard Marx

5:48 pm on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not being belligerent but:

>> the server runs any cf tags and then renders up html

So, the CFML tags are turned into script and HTML. No CF tags are left behind in the final out to the client (I still reckon). I suppose this question could be answered by doing a view > source on a page in a browser that has been processed by CF on the server.

Pudding proof.

Sticky me a URL if you have one.

The gist is that, if you have your tag:

<cfset myVar = myArray[0]>

This is processed on the server before myArray has been defined - as it's defined in the client. Do you know what kind of HTML is finally output by this tag?

I can imagine how it would all pull together, but seeing just in what manner it's going wrong may help (the actual current output). It may be something as simple as the value needing to be quoted - presumeably CFML is XML compliant.

kmbarz

7:20 pm on Aug 25, 2004 (gmt 0)

10+ Year Member



"<cfset myVar = myArray[0]>
This is processed on the server before myArray has been defined - as it's defined in the client."

Right, this happens before this gets to the client which is why this statement won't work. Let me ask you this (and thank you, you've been great to help), as I can pass form information onto my next CF page (page1action.cfm), if I have a form on the client's first page (after rendering by CF), would there be a way to assign myArray[0] to a hidden input tag within my form (preferably by looping through the array and dynamically creating all the tags I would need)?

say:
<form>
<input type="hidden" name="var0" value="myArray[0]">
<input type="hidden" name="var1" value="myArray[1]">

if so, on page1action.cfm, then I could say:
<cfset myvar0=#form.var0#>
<cfset myvar1=#form.var1#>...

The number of variables in the array will change, so I'd need to dynamically generate the input tags.
Thanks again.

Bernard Marx

9:10 pm on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



to a hidden input tag within my form (preferably by looping through the array and dynamically creating all the tags I would need)?

Yes, definitely.
Let me have a look again. Meanwhile,
- What are the the values (in meaning) - strings or numbers (both)?
- Is there the CF version of the javascript, eval, where you can execute code/data structure that is input as a string?

kmbarz

5:20 pm on Aug 27, 2004 (gmt 0)

10+ Year Member



Well, worked on it late that night and finally found a solution that worked. Instead of capturing it into a JS array, I just write out to the page some new hidden input tags. Then CF can grab it when it goes to the action page. Here's the portion of the code that does it:

var NewMessage =
CMNum + "&nbsp;&nbsp;&nbsp;" +
CMStartDate + "&nbsp;&nbsp;&nbsp;" + ... +
'<input type="Hidden" name="CMVar' + CMCount + '" value="' + CMNum + '">' +
'<input type="Hidden" name="CMVar' + CMCount1 + '" value="' + CMStartDate + '">' + ...

CMOutMessage = CMOutMessage + NewMessage;
CMOut.innerHTML = CMOutMessage;

Then on the page1action.cfm, you can access the first variable as <cfset CMNum = #form.CMVar1#>

So again thanks, you got me on the right track.