PDA

View Full Version : Coldfusion help needed. Is it possible to pass variables?


noobie2005
11-30-2005, 07:15 PM
Hi,

Thanks alot for those who helped me out on my last issue. I was wondering if anyone out there knew any ColdFusion and if this was possible to do:

If it was possible to pass a total from a javascript calculation to a <cfset> variable. Something like this ex:

javascripttotal = number1 + number2

then:

<cfset cfvariable = javascripttotal>
Then insert cfvariable into a database. I hope that's understandable. Thank you in advance

oracleguy
11-30-2005, 07:18 PM
You would need to send it via query string to the page, which means another page (or the same) would have to load again so that CF could get the result and insert it into the database.

You can't avoid a page reload since CF is on the server side and is executed before it gets to the user where the JS is ran.

noobie2005
11-30-2005, 10:23 PM
Well, I used hidden fields and tried to pass them that way, but to no avail.

I keep getting an error that the specified form does not exist. I'll attach the code that I have so far. Maybe there's something that I'm overlooking?


<script language="JavaScript" type="text/javascript">
<!--
function totmnthlyincome(frm)
{
var mnthlyincome_1 = document.form.monthlyincome_1.value
var mnthlyincome_2 = document.form.monthlyincome_2.value

tot_mnthlyincome = Number(mnthlyincome_1) + Number(mnthlyincome_2)
document.form.total_monthlyincome.value = tot_mnthlyincome.toFixed(2)
}

....

<form name="form" action="budgetform2.cfm" method="post">
<tr>
<td><span style="font-size:13px;">INCOME</span></td>
</tr>
<tr>
<td>Enter your monthly net income</td>
<td valign="bottom"><input type="Text" name="monthlyincome_1"></td>
<td rowspan="4" width="230"><!--- <cfgraph type="PIE" query="budget_pie"></cfgraph> ---></td>
</tr>
<tr>
<td>Enter spouse/other net income</td>
<td><input type="Text"name="monthlyincome_2" onChange="totmnthlyincome(this.form)"></td>
</tr>
<tr>
<td>Total monthly net income</td>
<td><input type="Text" name="total_monthlyincome" onFocus="this.form.elements[0].focus()"></td>
</tr>
<tr>
<td colspan="2" valign="top" align="left">
<table border="1" width="100%" class="title" valign="top" align="left">
<tr>
<td><span style="font-size:13px;">EXPENSES</span></td>
</tr>
<tr>
<td width="145">To Enter Montly Expenses</td><td><a href="budgetform2.cfm" onClick="popUp(this.href,'elastic',530,780);return false;"><input type="image" name="submit" src="images/budget_btn.gif" border="0"></a></td>
</tr>
<tr>
<td>Total Monthly Expenses</td><td><input type="Text"></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="image" name="submit" src="images/calc_btn.gif" border="0"></td>
</tr>
</table>
</td>

.....

<cfoutput>
<input type="Text" value="#form.total_monthlyincome#">
</cfoutput>
<table cellspacing="2" cellpadding="2" border="1" align="center" width="600" class="title">
<form name="form2" action="budgetform2.cfm" method="post">
<tr>
<td><span style="font-size:13px;">INCOME</span></td>
</tr>
<tr>
<td>Enter your monthly net income</td>
<td valign="bottom"><input type="Text" name="monthlyincome_1"></td>
<td rowspan="4" width="230"><!--- <cfgraph type="PIE" query="budget_pie"></cfgraph> ---></td>
</tr>
<tr>
<td>Enter spouse/other net income</td>
<td><input type="Text"name="monthlyincome_2" onChange="totmnthlyincome(this.form)"></td>
</tr>
<tr>
<td>Total monthly net income</td>
<td><input type="Text" name="total_monthlyincome" onFocus="this.form.elements[0].focus()"></td>
</tr>
<tr>
<td colspan="2" valign="top" align="left">
<table border="1" width="100%" class="title" valign="top" align="left">
<tr>
<td><span style="font-size:13px;">EXPENSES</span></td>
</tr>
<tr>
<td width="145">To Enter Montly Expenses</td><td><a href="budgetform2.cfm" onClick="popUp(this.href,'elastic',530,780);return false;"><input type="image" name="submit" src="images/budget_btn.gif" border="0"></a></td>
</tr>
<tr>
<td>Total Monthly Expenses</td><td><input type="Text"></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="image" name="submit" src="images/calc_btn.gif" border="0"></td>
</tr>
</table>

oracleguy
12-01-2005, 01:04 AM
Is coldfusion saying that or the javascript?

SteelValor
12-01-2005, 04:04 PM
Can you post a url so we can see exactly what you're doing? You should also secure you CF code so it can't be manipulated by end users. ie: cfqueryparam, cftry/catch, isnumeric, etc.

noobie2005
12-01-2005, 04:33 PM
http://isminternet.com/danlopez/budgetform.cfm
(http://isminternet.com/danlopez/budgetform.cfm)
Click 'Click Here' and you'll see whats going on. I got a suggestion to use a URL variable, but I've never used that before. And thanks for the suggestion on secureing my code, I've got to do that. Thanks again for the help guys! :thumbsup:

noobie2005
12-02-2005, 04:32 PM
Hey guys,

Nevermind this issue, I got it working. But, I have another question.
If your writing a <cfscript> is it possible to pass form variables to variables in the script?

Heres what I mean:


<input type="hidden" name="total" value="#URL.value#"><input type="text" name="income" value="#URL.value#">
-
<input type="text" name="amt" onChange="totalthis(this.form)"> =
<cfscript>
function totalthis()
{
var totalmonthlyincome = #form.total#;
var amount = #form.amt#;
newamount = Number(totalmonthlyincome) - Number(amount);
document.form.newamt.value = newamount.toFixed(2);
}
</cfscript>
<input type="Text" name="newamt" onFocus="<cfoutput>#totalthis()#</cfoutput>">


Would that be right? With that code, I get the following error:
Element TOTAL is undefined in FORM.

Any help is appreciated. Thanks again!

UPDATE: I've changed the "var totalmonthlyincome = #form.total#;" to "var totalmonthlyincome = #URL.valuel#;" which got rid of my error on that line, but I get the same error, this time regarding "var amount = #form.amt#;"

ccl
12-04-2005, 06:12 AM
onFocus="<cfoutput>#totalthis()#</cfoutput>" will not work because you are trying to execute the CF in the browser and CF runs on the server.
You will have to use just javascript for any calculating and dynamic output that you want to do without submitting back to the server.