ColdFusion variable not set properly - coldfusion

I have a form with some html elements. It has a check box which is by default checked. On click of the submit button it calls the submitForm function
Based on the checkbox condition it has to do the action. If the checkbox is Y, then it has to do one form action and if not checked then another action. Using javascript I have checked whether its checked or not. But I am not able to set the coldfusion variable for this. Always it overwrites the variable.
Below is the code snippet
This is the ColdFusion variable which is used. This is by default set to Y
<cfset form_condn_var = 'Y'/>
function submitForm(){
if (document.getElementById('Show_Final_Model').checked)
{
form.Show_Final_Model.value = 'Y';
}
else{
<cfset form_condn_var = 'N'/>
}
}
<cfif '<cfoutput>#form_condn_var#</cfoutput>' eq 'Y'>
<form id="form1" action="test.cfm" method="POST" target="testmain">
<cfelse>
<form id="form1" action="<cfoutput>#something#</cfoutput>" method="POST" target="_blank" onSubmit="">
</cfif>
It always set the variable form_condn_var as N and it goes to the else condition of the form irrespective of the condition. . But when I alert the value its comes correctly.
I cannot use hidden variable also as the form is not being called initially. Based on the checked condition only it is accessed.
Could somebody please tell me why the form_condn_var gets overwritten irrespective of the condition being checked.
Or is there any other way I can achieve this?
Thanks in advance

Short version
If I have understood your logic correctly then you can replace all of what you pasted with the following <cfif>
<cfif IsDefined('form.Show_Final_Model') AND form.Show_Final_Model EQ 'Y'>
<form id="form1" action="test.cfm" method="POST" target="testmain">
<cfelse>
<form id="form1" action="<cfoutput>#something#</cfoutput>" method="POST" target="_blank" onSubmit="">
</cfif>
Explanation
Your main problem here is that you have got Javascript outside of tags with a coldfusion within them and seem to be confusing what each language does.
First of all Coldfusion renders the HTML. As far as Coldfusion is concerned, the javascript if statement is just text, and so it sees the following logic
<cfset form_condn_var = 'Y'>
<cfset form_condn_var = 'N'>
<cfif form_condn_var EQ 'Y'> <!--- form_condn_var === '<cfoutput>#form_condn_var#</cfoutput>' but much cleaner --->
.......
<cfelse>
.......
</cfif>
In turn leading to the following HTML being rendered
function submitForm(){
if (document.getElementById('Show_Final_Model').checked)
{
form.Show_Final_Model.value = 'Y';
}
else{
}
}
<form id="form1" action="<cfoutput>#something#</cfoutput>" method="POST" target="_blank" onSubmit="">
I suspect in your example you trimmed out some logic though as otherwise that Javascript would be output as plaintext, as it is not within <script> tags.

If you are submitting your checkbox and then setting the form action for the next form (form 2 - after you've chosen the checkbox) I think what you want to do is the following:
<cfparam name="form.Show_Final_Model" default="N"/>
<cfif form.Show_Final_Model IS 'Y'>
<form id="form1" action="test.cfm" method="POST" target="testmain">
<cfelse>
<form id="form1" action="<cfoutput>#something#</cfoutput>"
method="POST" target="_blank" onSubmit="">
</cfif>
This would be on the handler page (after the form is submitted). You would not need the JS function.
If however you are trying to change the action param of your form to something else based on the check or uncheck of the checkbox (in other words - withing the same form) then ALL of your code needs to be javascript and CF has little to do with it. Set 2 variables - action a and action b, check the value of the checked form element and change the form.action value to whichever one you want.
But most importantly get settled in your mind on what is "server side" and what is "client side" .. that's where you are slipping up. good luck :)

I'm not sure if I'm understanding the question correctly, but it would seem that this situation could be handled purely on the action page based on conditional processing based on the result of the form checkbox value. Instead of sending the form to two different action templates based on the value of the checkbox, just process the form accordingly on the action template based on conditional processing of the checkbox form value.

Related

coldfusion cffile upload replace no file selected

In Coldfusion, using cffile upload, when a file is selected with the browse button the name of the selected file shows next to the button. So far, so good. When I submit the file for upload, the text changes back to "no file selected". This is doubtless because we are submitting a form.
However, I'm submitting the form to the same page, and would like to see the name of the file selected instead of "no file selected" which is likely to confuse the user.
The code:
<form
enctype="multipart/form-data"
method ="post"
name ="attupload"
action ="">
<cfoutput>
<table style = "margin-left: 40%">
<tr><td>
<input name="theupload"
type="file"
style = "font-family: comic sans ms;
color: ##679C9C">
</td></tr>
</table>
<div style = "width:20%; margin:5% auto 5% 45%">
<input name = "submit"
type = "submit"
class = "submitbut"
style = "font-size: 16px"
value = "Upload File">
</div>
</cfoutput>
</form>
The words 'no file selected' seem to be part of the input name = 'upload' field, but ColdFusion evidently takes control of it, and I cannot insert anything else.
Does anyone know how I can control the 'no file selected' note, so that I can put the name of the selected file back in that spot? I have tried with javascript, but it is simply ignored.
Once a file has been selected, you can access its properties via JavaScript with the File API. You can't affect the value displayed next to the default <input type="file"> field with JavaScript.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file
You should also avoid submitting forms to the same page. You should follow the process:
Form page
HTTP POST to form processing page.
after form processing, <cflocation> (301 redirect) back to a list page or the form page.
When you post to the same page, the user can hit reload and be prompted to resubmit form data. This can be problematic in many situations. As an alternate, you can post the form data using Ajax or better, via Fetch. This avoids a re-post on page reload and redirecting away from the current screen.
Also, you should avoid naming an <input type="submit"> "submit". It can conflict with the JavaScript function submit() should you need to use it.
<input name = "submit"
type = "submit"
class = "submitbut"
style = "font-size: 16px"
value = "Upload File">

Coldfusion - How to prevent multiple clicks?

I have a button (anchor tag) that send a confirm message if you press it.
The problem is that for example if you press it 5 times very quickly it will send 5 confirm messages, if you press it 2 times it will send 2 messages.
This can occur when the user has low connection speed and while the page is refreshing he presses again the button.
How can I manage this situation? I though of disabling the button but for other reasons this is not possible.
<a class="msg" href="/manage/conversations.cfm?destination=#destination#">
#ucase(request.l('Send'))#
</a>
Thank you for your time
Ultimately, you need to have code on your server to prevent processing the link multiple times from the same user.
However, to solve the UI issue, have you link call a function instead of the cf file directly.
<a class="msg" href="javascript: processLink(#destination#);">
#ucase(request.l('Send'))#
</a>
<script>
runCount = 0;
function processLink(destination){
runCount++;
if (runCount == 1){
window.location.href = "/manage/conversations.cfm?destination=" + destination;
}
}
</script>
As mentioned in the previous answer it's nice to have some client side javascript to stop duplicate submissions from trigger happy users however you should also do this checking server side.
One approach would be to create a hidden formfield with a GUID that coldfusion generates when coldfusion renders your form.
So something like:
<cfset GUID = createUUID()>
<cfoutput>
<form id="frm" action="/target.cfm" method="post">
<input type="hidden" name="guid" value="#GUID#">
<!-- all your formfields go here -->
<input type="submit">
</form>
</cfoutput>
On the server side the target page then checks if it has already previously received the GUID. There are lots of ways to do, here are two of many ways.
a) Use Session Scope.
This is probably the quickest way if you are not running in a clustered environment and just need something quick for a tiny application.
<cfif isDefined("session.MYPAGE_GUID") AND session.MYPAGE_GUID EQ form.guid>
<cfoutput>Duplicate Form Submission</cfoutput>
<cfabort>
<cfelse>
<cfset session.MYPAGE_GUID = form.guid>
<!-- Do Business Logic Here -->
</cfif>
b) Use a Database Table.
Create a database table with a column called GUID. Make sure that GUID is the primary key or has a unique constraint.
Before you run your business logic insert the form.GUID into the database table. If you can do the insert process your business logic, if not the database query will throw an error that the record exists. You can then catch this error and take the appropriate action for a duplicate submission.
I prefer the database option as it works across clustered environments and database server are solid protecting against race conditions to ensure that a GUID is only set once.
Please be aware that this is just demonstrating the basic concepts and is not a drop in solution. There is a bit of more work to get these concepts into an e-commerce solution.
The best way is to disable the link once it's selected. If you don't want to do that, an alternative is to structure conversations.cfm like this.
<div id="pageContent">
small amount of text
</div>
<cfflush>
</body>
</html>
<cfsavecontent variable = "actualPageContent">
code
</cfsavecontent>
<cfoutput>
<script>
var #toScript(actualPageContent, "newPageContent")#;
document.getElementById("pageContent").innerHTML = "newPageContent";
</script>
</cfoutput>

Passing two form variables in a single URL

I'm running the following form inside abc.cfm.
// Parameters Defined
<cfparam name="startdate" default="#DateFormat(dateAdd('d',-40,now()), 'yyyy-mm-dd')#">
<cfparam name="enddate" default="#DateFormat(dateAdd('d',-1,now()), 'yyyy-mm-dd')#">
<cfform format="HTML" action="datedownload.cfm" method="get" >
<cfformgroup type="horizontal">
<cfinput type="dateField" name="startdate" width="100" value="#startdate#">
<cfinput type="dateField" name="enddate" width="100" value="#enddate#">
<cfinput name="submitApply" type="submit" value = "Apply">
<cfinput type="button" name="download" value="Download" onclick="window.location.href='datedownload.cfm?startdate=#form.startdate#&enddate=#form.enddate#path=http://abc.xyz.com/username/July30/datedownload.cfm'">
</cfformgroup>
</cfform>
Everything is printing fine with the following code in datedownload.cfm
Startdate: <cfdump var = "#startdate#">
End Date :<cfdump var = "#enddate#">
Except that, the Enddate is printing full path along with it as follows:
Startdate: 2013-06-20 End Date : 2013-07-29path=http://abc.xyz.com/username/July30/datedownload.cfm
How can I remove the stuff starting from path?
If I am reading this correctly, you are getting an error that startdate and enddate are not defined in the form scope when you try to load download.cfm. Since you are passing those variables to download.cfm as part of a query string (by submitting the form using GET), they would not be present in the form scope.
I can think of 2 quick and easy solutions:
First, you can change your reference to form.startdate and form.enddate to url.formdate and url.enddate respectively. Variables passed in as part of the query string (like when you do a GET) become part of the url scope, not the form scope (liek when you do a POST).
Second, you can param the variables like this in download.cfm:
<cfparam name="url.startdate" default="#DateFormat(dateAdd('d',-40,now()), 'yyyy-mm-dd')#">
<cfparam name="url.enddate" default="#DateFormat(dateAdd('d',-1,now()), 'yyyy-mm-dd')#">
<cfparam name="form.startdate" default="#url.startdate#">
<cfparam name="form.enddate" default="#url.enddate#">
This will first param the values in the url scope to the same values you have in the page that displays the form, then it will param the same variable names in the form scope to the same value of the same variable names in the URL scope.
Use an ampersand before enddate instead of the question mark and add an ampersand before the path variable
window.location.href='Download.cfm?startdate=#form.startdate#&enddate=#form.enddate#&path=http://abc.xyz.com/<username>/Testing/Testing/Download.cfm'
The simplest way to solve your problem is to get rid of the 2nd button. It is not necessary and will confuse not only you, but your users. Since your form method is "get" the two formfields will be part of the url scope which seems to be what you want.
Also, where are the form variables coming from in the value attributes of your two inputs?
What's wrong with using a form post? That's the way I prefer to do it. I also test the request type (POST versus GET) to ensure that the download file isn't bookmarkable.
You'll need to use javascript to get the dates in the web-based form, not ColdFusion. (The user will also need to have javascript enabled to use the form to use location.href.)
Give your form fields matching IDs and try the following:
window.location.href='Download.cfm?startdate='+ document.getElementById('startdate').value +'&enddate='+ document.getElementById('enddate').value +'&path=http://abc.xyz.com/<username>/Testing/Testing/Download.cfm';
I'd recommend not using CFForm tags since they require the the /CFIDE/ directory and is currently recommended to be blocked:
Secure CFIDE Directory for ColdFusion
ColdFusion 9 Server Lockdown Guide (PDF)
ColdFusion 10 Server Lockdown Guide (PDF)
Make sure you perform date validation on the server-side. If you need client-side date validation, you can use HTML5 DOCType and the attributes type="date" & required or consider using the jQuery Validation plugin (preferable to CFForm validation).

No need of CGI.script_name

Please refer to the following code:
<cfform method="POST" action="#CGI.script_name#">
<p>Enter your Name:
<input name="name" type="text" hspace="30" maxlength="30">
<input type="Submit" name="submit" value="OK">
</cfform>
<cfscript>
function HelloFriend(Name) {
if (Name is "") WriteOutput("You forgot your name!");
else WriteOutput("Hello " & name &"!");
return "";
}
if (IsDefined("Form.submit")) HelloFriend(Form.name);
</cfscript>
Source: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=UDFs_01.html#1167055
The code runs fine even without CGI.script_name attribute of action field. May I know why it's required then? The description says "Uses the script_name CGI variable to post to this page without specifying a URL. "
The default action of an HTML form is to submit to itself when no action is specified. See this related discussion on the topic: Is it a good practice to use an empty URL for a HTML form's action attribute? (action="")
I always include an action, if for no other reason, to avoid confusion.

How to encode HTML form in coldfusion?

I'm new to coldfusion.
I have page called test1.cfm
<form action = "test2.cfm" method = "post">
Type your name and hit submit:<br>
<input type = "Text" name = "txt1">
<input type = "Submit" name = "" value = "submit">
</form>
And test2.cfm
<cfoutput>
success
#form.txt1#
back
</cfoutput>
Now I want to know if we can encode the displaying of text value in test2.cfm page.
For eg. if we type <br> in the textbox, test2 page will render it as html, and I want to stop rending and it should display as text.
Thanks
It's HTMLEditFormat(), not code.
Use
#htmlCodeFormat(form.txt1)#