Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Taking a value from a javascript variable and setting it in Cold Fusio

Status
Not open for further replies.

jeepxo

Programmer
Oct 1, 2002
228
CA
I do some things in a popup window and pass the value back through a javascript function.

Is there a way that I can take the value that is passed back and set it as a coldfusion variable.

what I am specifically looking for is something that will do this.


function changingValues(id, valueReturned)
{
<cfset myColdFusionValue_id = valueReturned>
}

I need to append the id to &quot;myColdFusionValue_&quot;
and set that variable value = valueReturned
 
I think a visual explanation is easier than a written:

<body>
<SCRIPT LANGUAGE=&quot;JavaScript&quot; TYPE=&quot;text/javascript&quot;>
<!--
function test(){
document.form1.inputname1.value=&quot;Testing&quot;;
return true;
}
//-->
</SCRIPT>
<form action=&quot;thispage.cfm&quot; method=&quot;post&quot; name=&quot;form1&quot; id=&quot;form1&quot; onSubmit=&quot;return test()&quot;>
<input name=&quot;inputname1&quot; type=&quot;Hidden&quot; value=&quot;&quot;>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;send&quot;>
</form>
<cfif #IsDefined(&quot;form.inputname1&quot;)#>
<cfoutput>#form.inputname1#</cfoutput>
</cfif>
</body>

Basically, you create a form and use javascript to assign a value to the a field.


The only dumb questions are the ones that are never asked
 
hmmm...
that isn't quite what I was looking for.

I have a form that is populated from a database with the names of the input boxes generated dynamically.

I have 41 choices that the user may select for one of the comment fields in for each record from the database. The user may make multiple selections for this comment field.

Since the table is generated dynamically I may have 5 comment fields or 50 comment fields that could each use 1 or more of the 41 choices.

What my thought was, was to have a button that opened another page with theses 41 comments listed as check boxes. The user checks the appropriate boxes, then presses submit. This then updates the comment field on the first page.

My intention was to do the submission of the comments client side to reduce traffic to the server.

Currently what I have then is this
<cfset counterVariable=0>
<cfset comments = ArrayNew(1)>
<cfoutput Query blahblahblah>
<cfset counterVariable= counterVariable +1>
<cfset comments[#counterVariable#] = someDatabaseTable>

<textarea name=&quot;comment_#counterVariable#&quot;>#comments[&quot;#counterVariable#&quot;]#<textarea>

</cfoutput>

This works quite well when loading out of the database.

What I want to do without leaving the page is dynamically update the #comments[&quot;#counterVariable#&quot;]#


I have the code to generate a string in Javascript based on the checkboxes.

I can pass that back to the first page in Javascript.
What I need to do now though is

take that javascript and place it in the cold fusion...hence my original post.

What you are suggesting won't work this way.

So what I am looking for is a way that I can dynamically change the #comments[&quot;#counterVariable#&quot;]#

I have the string in javascript.

I know what location in the comments array that it needs to be put, I just can't seem to find a way to make this change without having some more user interaction.
 
Since ColdFusion is server-side, and javascript is (typically) client-side, all your ColdFusion has already finished processing by the time any of your javascript runs.

So there's no way for Javascript to directly affect ColdFusion code or variables.

What twcman was suggesting is the only way to get the job done. Use javascript, as you're doing, to open the pop-up, collate the data from the checkboxes, then store your checkbox data in a hidden field in the main form. Then the user must submit the main form with the hidden field(s) - say it's to &quot;confirm the requested changes&quot; or something - upon which your action page would find all the hidden fields, parse out and manipulate the javascript strings (if necessary) and do an INSERT or UPDATE back into your database accordingly.
Hope it helps,
-Carl
 
I can't accept your explanation at all carl because if that was the case then nothing that is placed in input boxes would be returned to the server when a submit button is pressed.

This
<textarea name=&quot;comment_#counterVariable#&quot;>#comments[&quot;#counterVariable#&quot;]#<textarea>

displays the value that I pull from the database. I can manually edit the text area and have that submitted to the database.

This is an EXTREMELY simple task in ASP and I can't imagine that a platform as widely used as coldfusion is not capable of doing something along this line.

Mostlikely my problem stems from the fact that I know so very little of coldfusion and as such am not approaching this problem from the right angle.

the majority of the task that I have seen in coldfusion have not been intuitive in the least in their solution and I find it rather cumbersome when combining Client side and server side scripting.

 
Wait... hold on there... you're blaming ColdFusion when the real culprit here is Javascript.

Taking your example, when you code something like this in your HTML:

Code:
<cfoutput><textarea name=&quot;comment_#counterVariable#&quot;>#comments[&quot;#counterVariable#&quot;]#<textarea></cfoutput>

ColdFusion intercepts the code before the browser ever sees it (intercepts it on the server-side), parses out all CFML and processes it (replacing #counterVariable# with the value of the variable counterVariable, performing any flow-control, etc, etc).

If you look at the HTML source (in your browser) for a CFM page once it's rendered, you'll see that there's absolutely no CFML left in it... it's all been turned into standard HTML so the browser will understand it. All of this is done BEFORE the browser ever sees the page.

And since it's the browser that interprets and processes javascript, javascript can't possibly run until after all of the ColdFusion has completed (I suppose one could do something interesting with CFFLUSH... but that'd probably just turn into an unreliable mess).

So, to the browser, the above looks like:
Code:
>
<textarea name=&quot;comment_8&quot;>Hello world<textarea>
and when the form is submitted, the value of textarea comment_8 is, indeed, submitted just as if it were a standard value in a static form field (ie - the form submits &quot;Hello world&quot;, not &quot;comments[&quot;#counterVariable#&quot;]&quot;... the browser doesn't even know what comments[&quot;#counterVariable#&quot;] is).

Now you can manually edit the text in the field, click submit, and ColdFusion can easily update the database with the new value (this is an extremely simple task in ColdFusion as well... I dare say simpler than doing it in ASP). But that wasn't the situation that you originally explained.

You said that you wanted to be able to somehow open a pop-up window, have the user check the appropriate checkboxes, close the window and have javascript take the results of what was checked in the pop-up and update the database or the variable name using ColdFusion. That's just not possible. In order for ColdFusion to update the database, it needs to be the first thing that processes (same with ASP)... and if it's the first thing that processes, your javascript variables would be empty (the javascript parser wouldn't even yet be active).

The only way you can have javascript affect ColdFusion variables is to store the javascript values in a form field and submit the form... at which time ColdFusion can grab the values that are in the field (notice, I didn't say grab the javascript variables... they wouldn't exist anymore).


Now... you can do some tricky things, like include values of ColdFusion variables in your javascript... that might make it seem like your ColdFusion can be manipulated by javascript. Something like:

Code:
function setElementValue(theID, theValue)
{
    document.theID.value = theValue;
}
then, where you actually call the function, you can wrap some CFML inside:
Code:
<cfset intendedValue = &quot;hello&quot;>

<cfoutput><a href=&quot;javascript:void(0)&quot; onclick=&quot;javascript:setElementValue(someElement,'#intendedValue#')&quot;>Change the value</a></cfoutput>

and now when you click on the link, the value of the element is set to the value of a ColdFusion variable... but in the end, it's all just HTML that's assembled on the fly.

There's no possible way that you'd be able to get something like:
Code:
function changingValues(id, valueReturned)
{
    <cfset myColdFusionValue_id = valueReturned>
}
to work, because by the time changingValues() was called, myColdFusionValue_id would have already been set (and, in fact, the line &quot;<cfset myColdFusionValue_id = valueReturned>&quot; wouldn't even exist anymore.



Of course... you can also run javascript server-side... but server-side Javascript is not for the faint-hearted... and I don't believe it's capable of opening client-side pop-up windows, etc.
Hope it helps,
-Carl
 
I'm new with coldfusion too but understand both asp and javascript fairly well. Sounds like what you want to do is submit the popup to update the database, then reload the opener.

when you submit the pop up
after all of the DB writing write a javascript function that does a window.opener.reload() (i think would be the syntax.) then self.close(). That would display any changes in the pop up window but you would loose any changes to form fields to the parent window.

maybe submit the opener
then submit the popup
then reload the opener
then close the popup.

but I could be abstract in my thinking.
 
That could be interesting.

Have the pop-up window submit to itself, and when FORM.submit is true, do your DB update, then reload the parent and close the pop-up.

Good one bombboy! Hope it helps,
-Carl
 
Thanks csteinhilber.
jeepxo, I don't know what you mean by this being a very simple task in ASP. csteinhilber's above novelette was perfectly acurate. The only way to get client side variables to the server is to either put it in a querystring/url or http header (submit via post). Maybe we are not understanding what you are asking for. I use javascript to to add values to the url when I use a pop up window that is interperated by ASP. Like this...
winpops=window.open(&quot;themeinfo.asp?sID=&quot;+siteAdmin.editTheme.value+&quot;&quot;,&quot;&quot;,&quot;height=450,width=400,top=&quot;+centerHeight+&quot;,left=&quot;+centerWidth+&quot;,toolbar=0,location=0,directories=0,status=0,scrollbars=1,menubar=0,resizable=0,&quot;)

that url makes a small pop up window with a new form to edit a theme used by other users. editTheme.value is the selected theme from a drop down. ASP uses the querystring sID to tell what theme info to grab from the database. Demonstrating one of the two ways to get a client variable to the server.
 
It is trivial.
When the page is loaded I have a function server side that passes all of the values from the database to client side.

I manipulate the page client side, giving it a true dynamic feel without having to go back to the server until the final submit is pressed. At that point, I use the Request Object (as you would for any html page) to get the finished product.

VERY TRIVIAL to do.

Of course in ASP I am using Javascript both server side and client side.
 
Then we really aren't understanding what you're trying to do... as what you just described isn't what you originally asked about.

What you just described is absolutely trivial in ColdFusion, too.

&quot;When the page is loaded I have a function server side that passes all of the values from the database to client side.&quot;

Okay... don't quite know why you'd do this, but you could certainly set client-side variables (as you're describing) using ColdFusion.

Code:
<CFQUERY name=&quot;myQuery&quot; ...>
  SELECT coldFusionVar1, coldFusionVar2
    FROM myTable
       :
</CFQUERY>

<CFOUTPUT>
<javascript ...>
        :
   var clientsideVar1 = '#myQuery.coldFusionVar1#';
   clientsideArray = new Array(#myQuery.RecordCount);
   <CFSET arraySlot = 0>
   <CFLOOP query=&quot;myQuery&quot;>
        clientsideArray[#arraySlot#] = '#coldFusionVar2#';
        <CFSET arraySlot = arraySlot + 1>
   </CFLOOP>
        :
   document.write(clientsideVar1);
</javascript>
</CFOUTPUT>


&quot;I manipulate the page client side, giving it a true dynamic feel&quot;

You now have clientside variables... so you can do whatever you want with them via javascript or whatever else. Someday you'll have to explain &quot;a true dynamic feel&quot;, though.


&quot;without having to go back to the server until the final submit is pressed.&quot;

Ergo, there's no way to do what you originally asked using ASP, either... which is have (clientside) javascript somehow affect the database itself without a submit. That only occurs when &quot;the final submit is pressed&quot; and ASP begins processing again.

&quot;At that point, I use the Request Object (as you would for any html page) to get the finished product.&quot;

ColdFusion uses the FORM object (structure) on submits with a method of &quot;POST&quot; and the URL object on submits with a method of &quot;GET&quot;... but, again, it's just as trivial as it is with ASP.

Code:
<CFQUERY name=&quot;myQuery&quot; ...>
  INSERT into myTable
    (coldFusionVar1,coldFusionVar2) VALUES
    ('#FORM.clientSideVar1#', '#FORM.clientSideVar2#')
       :
</CFQUERY>

I defy you to come up with a way that's any easier in ASP. In fact, I defy you to come up with a way that's even as easy as it is in ColdFusion. For one... notice there's no creating connections or recordset objects... CFQUERY does all that for you... and there's no recordset.moveNext or any of that... CFOUTPUT handles all of that.

Hope it helps,
-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top