Setting a form variable inside the form (coldfusion) - coldfusion

I am trying to manually set a variable INSIDE a form, because it contains html and placing it in the value attribute of a tag would cause errors in the display. Currently, I check to see if that attribute contained html, and if so, the field is empty.
I'd like to be able to set the variable as the old value if it contained html in previous entries of the form, so that the user wouldn't have to enter that field every time they loaded that ORM object to edit.
Here's a snippet:
<cfif ("#dataobject.getField()#" NEQ "" AND Left(dataobject.getField(), 1) EQ "<")>
<cfscript>
temp = dataobject.getField();
temp2=temp;
temp2 = Insert("---", temp2, 0);
temp2 = Insert("<!", temp2, 0);
temp2 = Insert("--->", temp2, Len(temp2));
dataobject.setField(temp2);
</cfscript>
<label for="name">
Field:
</label>
<input type="text" name="Field" value="">
<button id="savefield" name="savefield">Save</button>
<cfif form.Field EQ ""><cfset form.Field = temp></cfif>
<cfscript>
dataobject.setField(temp);
</cfscript>
<cfelse>
<label for="name">
Field:
</label>
<input type="text" name="Field" <cfif ("#dataobject.getField()#" NEQ "")>value="#dataobject.getField()#"</cfif>>
<button id="savefield" name="savefield">Save</button>
</cfif>
The code I was trying to use:
<cfif form.Field EQ ""><cfset form.Field = temp></cfif>
Coldfusion throws an error saying that the FORM variable is undefined (which doesn't surprise me). The "savefield" button calls javascript that opens a window allowing the user to set the value, then closes. Should I put my code there instead?
-The inserts which turn the string into a comment was an early attempt at a workaround that didn't work :/

Ok hopefully the following pointers will help you going:
Verify that the field exists in the form via; structKeyExists(form, "field") or use cfparam to initialize a default value
Escape the value with HTMLEditFormat(dataObject.getField()) to escape any HTML code which breaks the html
So e.g.;
<cfparam name="form.field" default="" />
<label for="field">
Field:
</label>
<input type="text" id="field" name="field" value="#htmlEditFormat(form.field)#" />
<button id="savefield" name="savefield">Save</button>
Gl !

if you want to force a value into the form (or any) scope you'll want to use cfparam before you use it.
<cfparam name = "form.field" default = "">
<cfif form.Field EQ "">
<cfset form.Field = temp>
</cfif>
Essentially this is the same thing as
<cfif !structKeyExists(form,"field")>
<cfset form.field = "">
</cfif>

Don't forget that preservedata is your friend.
You don't have to worry about the value="" attribute if you populate the form field ahead of time.
<cfquery name="qry">
SELECT Field1,Field2
FROM table
WHERE ID=<cfqueryparam cfsqltype="cf_sql_integer" value="#url.ID#">
</cfquery>
<cfloop list="#qry.Columnlist#" index="FieldName">
<cfset form[FieldName] = HTMLEditFormat(qry[FieldName][1])>
</cfloop>
<cfform preservedata="yes">
<label for="Field1">Field One:</label>
<cfinput name="Field1">
<label for="Field2">Field Two:</label>
<cfinput name="Field2">
</cfform>

Related

Coldfusion check if input value blank then set in another input

So I've read a lot of answers but none work for me. I need to check and see if the input value is empty (blank), if it is, then put the value of another input to empty as well.
Here is the code:
<input name="Main" type="text" value="" autocomplete="off" />
<input name="Second" type="hidden" value="" />
So when the value of Main is empty, I want to set the value of Second the empty as well.
Here is what I tried:
<cfif len(trim(Main)) EQ 0>
<cfset Second= "">
</cfif>
With scope form
<cfif len(trim(form.Main)) EQ 0>
<cfset Second = form.Second>
</cfif>

Radio button not checked breaks page but works if checked

I have a simple form that I'm trying to pass data to which on the action page has an email that sends the data that I put into the form.
On my form page. I have a text field and two radio buttons.
My text field works if I don't put data into it. However, on my radio buttons the page works if I check it but doesnt work if I don't check it at all which results in my page breaking I'm not sure what I'm doing wrong? I want the page to process the default to "no" if I didn't check either radio buttons. Do I have to validate or something?
Here is my code.
<cfparam name="form.firstName" default="">
<cfparam name = "form.optradio1" default="no">
<form action="test.cfm" method="post">
<label for="firstName"></label>
<input type="text" name="firstName">
<input type="radio" name="optradio1" Value="Male" <cfif form.optradio1 eq "Yes">checked</cfif>>
</form>
That's how radio and checkbox inputs work in HTML. If they are not checked, they are not submitted on the form submit.
To determine if the radio input was checked, you can use
structKeyExists(form, <name of the input, as string>) as in
structKeyExists(form, "optradio1").
<cfparam name="form.firstName" default="">
<form action="test.cfm" method="post">
<label for="firstName"></label>
<input type="text" name="firstName">
<input type="radio" name="optradio1" Value="Male" <cfif structKeyExists(form, "optradio1")>checked</cfif>>
</form>
Assuming you have two radio inputs:
<cfparam name="form.firstName" default="">
<form action="test.cfm" method="post">
<label for="firstName"></label>
<input type="text" name="firstName">
<input type="radio" name="optradio1" Value="Male" <cfif structKeyExists(form, "optradio1") and form.optradio1 eq "Male">checked</cfif>>
<input type="radio" name="optradio1" Value="Female" <cfif structKeyExists(form, "optradio1") and form.optradio1 eq "Female">checked</cfif>>
</form>
Your initial code doesn't work because:
if checked, form.optradio1 was equal to Male
if not checked, form.optradio1 was defaulted to no because of
<cfparam name = "form.optradio1" default="no">

Form counting every record when clicking submit?

I have a form that im outputting results of comments, it can more than one.
I can aprrove these comments all at once, but what I'm trying to do is make it
with the option to submit 1 comment at a time or all comments at a time.
The form at first I wrote it to work on 'submit all', so now with adding a submit
per comment also, I get some errors.
I get because every time I submit a comment (1 comment) its still looking for all the other comments
which I did not submit.
What I think is doing since txtTotalRecords= Mush2.Recordcount, it trying to find the
other records which weren't submitted.
I just can't figure out how I can change this to make it work.
Doing a cfdump on the form i get, meaning there are 11 comments to submit.
How would I be able to change RecordCount to take in every record by itself?
<cfparam name="FormSubmit" type="string" default="FormNotSubmitted">
<cfif isDefined("form.submit")><cfset FormSubmit = "FormSubmitted"></cfif>
<cfif isDefined("form.submit1")><cfset FormSubmit = "FormSubmitted1"></cfif>
<!--- Begin Content ================================================== --->
<cfif FormSubmit eq "FormNotSubmitted" || FormSubmit eq "FormNotSubmitted1" >
<form method="post" action="cse_execoffice_pending.cfm" name="review_comments">
<cfoutput>
<input type="hidden" name="txtApprovedBy" value="#GetCurrentUser.emp_id#">
<!-- count the records that come in from the pending -->
</cfoutput>
<cfoutput query="Mush3">
<form method="post" action="cse_execoffice_pending.cfm" name="review_onecomment">
<input type="hidden" name="txtTotalRecords" value="#Mush2.Recordcount#">
<hr>
<div class="comments_approvaldecision">
<p>
<CFDUMP VAR=#response_id#>
<input type="hidden" name="txtResponseID#mush2.CurrentRow#" value="#response_id#">
<input type="radio" name="execoffice_status#mush2.CurrentRow#" id="approve#CurrentRow#" value="1" checked="checked"> <label for="approve#CurrentRow#">Approve</label><br>
<input type="radio" name="execoffice_status#mush2.CurrentRow#" id="deny#CurrentRow#" value="2"> <label for="deny#CurrentRow#">Deny</label>
</p>
<p> </p>
<p>
<input type="radio" name="star#mush2.CurrentRow#" id="givestar#mush2.CurrentRow#" value="0" checked="checked"> <label for="givestar#CurrentRow#"></i> Give Star!</label><br>
<input type="radio" name="star#mush2.CurrentRow#" id="denystar#mush2.CurrentRow#" value="1"> <label for="denystar#CurrentRow#"></i> No Star</label>
</p>
</div>
</div>
<input type="submit" name="Submit1" value="Submit">
</form>
</cfoutput>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
</cfif>
<cfdump var="#form#">
<cfif FormSubmit eq "FormSubmitted" || FormSubmit eq "FormSubmitted1">
<!--- Get Form Values --->
<cfloop from="1" to="#txtTotalRecords#" index="j">
<h2>test</h2>
<cfset response_id[j] = #Trim(form["txtResponseID" & j])#>
<cfset execoffice_status[j] = #Trim(form["execoffice_status" & j])#>
<cfset star[j] = #Trim(form["star" & j])#>
<cfset commentpositive[j] = #Trim(form["txtCommentPositive" & j])#>
<cfset commentnegative[j] = #Trim(form["txtCommentNegative" & j])#>
<cfset commentpositivereReplace[j] = reReplace(commentpositive[j], '\n', '<br>', 'ALL')>
<cfset commentnegativereReplace[j] = reReplace(commentnegative[j], '\n', '<br>', 'ALL')>
</cfloop>
......... more code...
I believe the code you are getting errors on is not in your sample code listed on this page.
I'm assuming you are doing a to/from loop based upon form.txtTotalRecords.
What you'll want to do is loop over your form items looking for a specific partial form name.
something like this:
<cfloop list="form.fieldnames" index="i">
<cfif left(i,13) IS "txtResponseID">
<cfset thisID = replaceNoCase(i,"txtResponseID","")>
<cfquery>
UPDATE myTable
SET approve = <cfqueryparam value="#form["execoffice_status" & thisID]#">
WHERE ID = <cfqueryparam value="#thisID">
</cfquery>
</cfif>
</cfloop>

Passing <cfselect> hard coded values into a session scope

I have a cfform with multiple fields and I'm using an onpage post method to post information gathered from the user's entries into a session to pass onto each page of the registration process.
What I cannot figure out for the life of me and this is probably something simple is that if I have hard coded options for a how can I pass what is selected into the session? I can get a session.house.main.form.saletype is undefined error when trying to display the value selected on the next page. All my other form fields show up fine.
I removed all the other form fields to make it easier to check my code:
<cfif not structKeyExists(session.house, "main")>
<cfset session.house.main= {saletype="",
name=""}>
</cfif>
<cfparam name="form.saletype" default="#session.house.main.saletype#">
<cfparam name="form.name" default="#session.house.main.name#">
<cfif structKeyExists(form, "submit")>
<cfset session.house.main = {saletype=form.saletype,
name=form.name}>
<cflocation url="page2.cfm" addtoken="false" />
</cfif>
<cfform method="post">
<cfselect name="saletype" size="1">
<option value="Lease" selected>Lease</option>
<option value="Rent Now">Rent Now</option>
</cfselect><br />
<cfinput type="text" name="name" id="name" value="#form.name#" required="yes" message="Please enter your name"><br />
<cfinput type="submit" name="submit" id="submit" value="Save"><br />
</cfform>
Edit: Fixed cfselect name. How would I set the form.saletype into the cfselect with two hardcoded options?

ColdFusion form and session variable setting

I have a checkbox inside of a form tag and I basically want to persist the checked state of the checkbox using a session. Apparently I'm doing it wrong because whenever I reload the page it sets the session back to off(which is the default value for the checkbox param). Here is the code i'm using.
Form:
<cfform name="matureContent" method="post" action="/index.cfm?fuseaction=main.Channels_Detail&c=#URL.c#" enctype="multipart/form-data">
<cfif SESSION.matureSession eq "on">
<input name="myCheckbox" type="checkbox" checked="checked" />
<cfelse>
<input name="myCheckbox" type="checkbox"/>
</cfif>
<input type="submit" value="Save" />
</cfform>
Session variable and params if they are not present on page load.
<cfparam name="form.myCheckbox" default="off">
<cfparam name="SESSION.maturesession" default="off">
<cfset SESSION.maturesession = form.myCheckbox>
If i'm going about this completely the wrong way let me know. Thanks.
<cfparam name="form.myCheckbox"
default="off">
<cfparam name="SESSION.maturesession"
default="off">
<cfset SESSION.maturesession =
form.myCheckbox>
I think that will result in overwriting the saved value if you return to the page from somewhere else. Instead, try updating the session value only when the form was submitted. Also, since you are using a cfform you could shortcut things by using yes/no instead of on/off.
Update I forgot the cfparam for the session variable. But if you truly want to carry it throughout the session, you could also initialize it onSessionStart instead.
<cfparam name="SESSION.maturesession" default="no">
<cfif structKeyExists(FORM, "submit")>
<cfparam name="form.myCheckbox" default="no">
<cfset SESSION.maturesession = form.myCheckbox>
</cfif>
<cfform name="test" method="post" ....>
<cfinput name="myCheckbox" type="checkbox" value="yes" checked="#session.matureSession#" />
<input type="submit" name="submit" value="Save" />
</cfform>
Do you actually have sessions turned on?
You have to explicitly turn on sessions for your app using the CFAPPLICATION tag if you're using Application.cfm, or, if you're using Application.cfc, by setting this.sessionManagement = true.
Application.cfm:
<cfapplication
name = "application name"
applicationTimeout = #CreateTimeSpan(0,2,0,0)#
sessionManagement = "yes"
sessionTimeout = #CreateTimeSpan(0,0,20,0)#>
Application.cfc:
<cfcomponent output="false">
<!--- Application name, should be unique --->
<cfset this.name = "ApplicationName">
<!--- How long application vars persist --->
<cfset this.applicationTimeout = createTimeSpan(0,2,0,0)>
<!--- Should we even use sessions? --->
<cfset this.sessionManagement = true>
<!--- How long do session vars persist? --->
<cfset this.sessionTimeout = createTimeSpan(0,0,20,0)>
</cfcomponent>
OK, then if sessions ARE turned on, when you submit the form, what does the code look like that you're posting the form to?