Invoking a CFC from a FORM - coldfusion

Like the title says, I am invoking a CFC from a FORM. This is how I am doing it now, but there has to be a better way.
Form:
<cfform action="choice.cfm" method="post">
<cfinput type="hidden" name="method" value="DeleteMessage">
<cfinput type="hidden" name="announcement" value="#announcement#">
<cfinput type="hidden" name="ID" value="#ID#">
<cfinput type="submit" value="Delete" name="form.OKbutton1" onclick="return confirm('Are you sure you want to delete?');">
</cfform>
Action page:
<cfif IsDefined("form.OKbutton1")>
<cfinvoke component="pdprojects.scr.changedisablesysequipment"
method="DeleteMessage"
returnvariable="DeleteMessages"
argumentCollection="#Form#" />
</cfif>

I figured out a way to streamline this with jquery and cfc. Page updates fast, and no worries if the person hits back button or refreshes the page. You could probably do a lot more with JQuery to append a div tag instead of location.reload(); (Page Refresh) at the end of the call.
CF Call
<cfajaxproxy cfc="pdprojects.scr.changedisablesysequipment" jsclassname="choice_cls">
Here is the jquery
<script src="/js/jquery-1.7.1.min.js">
<script>
$(document).ready(function(){
$("#OKbutton").click(function(){
if ($('#Announcement').val() == "") {
alert("Please enter message in the text area!");
}
else {
var instance = new choice_cls();
instance.setForm("enterit");
instance.InsertMessage();
location.reload();
}
});
$("#OKbutton1").click(function(){
var instance = new choice_cls();
instance.setForm("deleteit");
instance.DeleteMessage();
location.reload();
});
});
Here is my html
<cfform action="" id="enterit" name="enterit" method="post">
<cfinput type="hidden" value="#session.username#" name="thisname">
<cftextarea cols="50" rows="8" name="Announcement"></cftextarea><br>
<cfinput type="button" value="Enter Announcement" name="OKbutton" id="OKbutton">
</cfform>
<cfform action="" id="deleteit" name="deleteit" method="post">
<cfinput type="hidden" name="method" value="DeleteMessage">
<cfinput type="hidden" name="announcement" value="#announcement#">
<cfinput type="hidden" name="ID" value="#ID#">
<cfinput type="button" value="Delete" name="OKbutton1" onclick="return confirm('Are you sure you want to delete?');">
</cfform>
Then just create your CFC for coldfusion!

Related

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">

ColdFusion not adding variable to url

Currently I'm in the works of creating a refrral system for my website, but somehow ColdFusion won't let me add a value to an URL.
When people sign up, they use a referral url which looks like this: /register/?referer=[name]. Followed by the following form:
<cfif IsDefined('URL.referer')>
<cfquery name="CheckRefer" datasource = "#DSN#">
SELECT username, ip_reg, look
FROM users
WHERE username='<cfoutput>#referer#</cfoutput>'
</cfquery> </cfif>
<form id="register_step_two" method="post" action="complete.cfm<cfif #referer# is not 0 and #CheckRefer.RecordCount# is 1 and #CGI.REMOTE_ADDR# is not #CheckRefer.ip_reg#>?referer=<cfoutput>#referer#</cfoutput></cfif>">
<input type="hidden" name="referer" id="referer" value="#referer#" />
<input type="hidden" name="email" id="email" value="#form.email#" />
<input type="hidden" name="bdday" id="bdday" value="#form.bdday#" />
<input type="hidden" name="bdmonth" id="bdmonth" value="#form.bdmonth#" />
<input type="hidden" name="bdyear" id="bdyear" value="#form.bdyear#">
<input type="hidden" name="username" id="username" value="" />
<input type="hidden" name="figure" id="figure" value="hr-834-61.hd-600-4.ch-3014-110.lg-3006-110-62.sh-3089-110.ca-3217-62-62,s-0.g-0.d-4.h-4.a-0,c201c1c1b7fceffd8d5e8f5204f593ef" />
<input type="hidden" name="password" id="password" value="#form.password#" />
<input type="submit" value="Registreer" id="register-button" onmousedown="this.style.backgroundColor='##ddd';" onmouseup="this.style.backgroundColor='##eee';" onmouseover="this.style.backgroundColor='##eee';" onmouseout="this.style.backgroundColor='##fff';" disabled="disabled" />
</form>
But after clicking the submit button it redirects me to complete.cfm and not complete.cfm?referer=[name]. I don't know why this happens, because I'm using the same method on other pages. All the variables inside the are defined...
For example: this form works perfectly fine (the code posted above is page 2 of 3 from my signup process, and this one underneed is the first one, page 3 is the complete.cfm).
<cfif IsDefined('URL.referer')>
<cfquery name="CheckRefer" datasource = "#DSN#">
SELECT username, ip_reg, look
FROM users
WHERE username='<cfoutput>#referer#</cfoutput>'
</cfquery> </cfif>
<form id="register_step_one" method="post" action="step2.cfm<cfif IsDefined('URL.referer') and URL.referer is not 0 and #CheckRefer.RecordCount# is 1 and #CGI.REMOTE_ADDR# is not #CheckRefer.ip_reg#>?referer=<cfoutput>#referer#</cfoutput></cfif>">
<label for="email">Email</label><br />
<input type="text" name="email" id="formemail" />
<div class="errormsg display_none" id="habbo_name_message_box">
<h3>Eoutmelding</h3>
Something went wrong..
</div>
<br /><br />
<label for="password">New password</label><br />
<input type="password" name="password" id="formpassword" />
<br /><br>
<label for="cpassword">Repeat password<label><br />
<input type="password" name="cpassword" id="formcpassword" />
<br /><br />
Date of birth:<br />
<select name="bdday" id="bdday" class="dateselector"><option value="">Dag</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option></select> <select name="bdmonth" id="bdmonth" class="dateselector"><option value="">Maand</option><option value="1">Januari</option><option value="2">Februari</option><option value="3">Maart</option><option value="4">April</option><option value="5">Mei</option><option value="6">Juni</option><option value="7">Juli</option><option value="8">Augustus</option><option value="9">September</option><option value="10">Oktober</option><option value="11">November</option><option value="12">December</option></select> <select name="bdyear" id="bdyear" class="dateselector"><option value="">Jaar</option><option value="2010">2010</option><option value="2009">2009</option><option value="2008">2008</option><option value="2007">2007</option><option value="2006">2006</option><option value="2005">2005</option><option value="2004">2004</option><option value="2003">2003</option><option value="2002">2002</option><option value="2001">2001</option><option value="2000">2000</option><option value="1999">1999</option><option value="1998">1998</option><option value="1997">1997</option><option value="1996">1996</option><option value="1995">1995</option><option value="1994">1994</option><option value="1993">1993</option><option value="1992">1992</option><option value="1991">1991</option><option value="1990">1990</option><option value="1989">1989</option><option value="1988">1988</option><option value="1987">1987</option><option value="1986">1986</option><option value="1985">1985</option><option value="1984">1984</option><option value="1983">1983</option><option value="1982">1982</option><option value="1981">1981</option><option value="1980">1980</option><option value="1979">1979</option><option value="1978">1978</option><option value="1977">1977</option><option value="1976">1976</option><option value="1975">1975</option><option value="1974">1974</option><option value="1973">1973</option><option value="1972">1972</option><option value="1971">1971</option><option value="1970">1970</option><option value="1969">1969</option><option value="1968">1968</option><option value="1967">1967</option><option value="1966">1966</option><option value="1965">1965</option><option value="1964">1964</option><option value="1963">1963</option><option value="1962">1962</option><option value="1961">1961</option><option value="1960">1960</option><option value="1959">1959</option><option value="1958">1958</option><option value="1957">1957</option><option value="1956">1956</option><option value="1955">1955</option><option value="1954">1954</option><option value="1953">1953</option><option value="1952">1952</option><option value="1951">1951</option><option value="1950">1950</option><option value="1949">1949</option><option value="1948">1948</option><option value="1947">1947</option><option value="1946">1946</option><option value="1945">1945</option><option value="1944">1944</option><option value="1943">1943</option><option value="1942">1942</option><option value="1941">1941</option><option value="1940">1940</option><option value="1939">1939</option><option value="1938">1938</option><option value="1937">1937</option><option value="1936">1936</option><option value="1935">1935</option><option value="1934">1934</option><option value="1933">1933</option><option value="1932">1932</option><option value="1931">1931</option><option value="1930">1930</option><option value="1929">1929</option><option value="1928">1928</option><option value="1927">1927</option><option value="1926">1926</option><option value="1925">1925</option><option value="1924">1924</option><option value="1923">1923</option><option value="1922">1922</option><option value="1921">1921</option><option value="1920">1920</option><option value="1919">1919</option><option value="1918">1918</option><option value="1917">1917</option><option value="1916">1916</option><option value="1915">1915</option><option value="1914">1914</option><option value="1913">1913</option><option value="1912">1912</option><option value="1911">1911</option><option value="1910">1910</option><option value="1909">1909</option><option value="1908">1908</option><option value="1907">1907</option><option value="1906">1906</option><option value="1905">1905</option><option value="1904">1904</option><option value="1903">1903</option><option value="1902">1902</option><option value="1901">1901</option><option value="1900">1900</option></select>
<br />
<div class="errormsg display_none" id="bd_err"><strong>Ongeldige geboortedatum.</strong></div><br>
<input type="hidden" name="referer" id="referer" value="#referer#" />
<input type="submit" id="reg_submit_button" value="Registreer" onmousedown="this.style.backgroundColor='#ddd';" onmouseup="this.style.backgroundColor='#eee';" onmouseover="this.style.backgroundColor='#eee';" onmouseout="this.style.backgroundColor='#fff';" />
</form>

select statement displays full list but not in a select statement

I am struggling to find out where I have gone wrong, and have spent hours trying to find out why this is happening, I think its just something I have missed but I cant spot it.
What happens is that the select statement instead of showing a dropdown which i can just select a team to delete displays all of the teams in a list with a delete button next to it, yet when I use the select statement on another page it works fine. if anyone can point out where I have gone wrong that would be appreciated
Cheers
<cfquery name="deleteteam" datasource="danny2">
SELECT *
FROM pool_teams
</cfquery>
<html>
<head>
<title>LCF Delete Team</title>
</head>
<body>
<cfif IsDefined('Form.delete_button')>
<cfoutput>
<form action="#CGI.SCRIPT_NAME#" method="post">
<input type="hidden" name="ID" value="#FORM.ID#"/>
do you really want to delete record?
<input type="submit" name="confirm_button" value="Yes">
<input type="submit" name="cancel_button" value="No">
</form>
</cfoutput>
<cfelseif IsDefined('FORM.confirm_button')>
<cfquery datasource="danny2">
DELETE FROM pool_teams
WHERE ID = '#FORM.ID#'
</cfquery>
The record has been deleted
<cfoutput> Return to list</cfoutput>
<cfelseif IsDefined('FORM.cancel_button')>
<cflocation url="#CGI.SCRIPT_NAME#" >
<cfelse>
<cfoutput query="deleteteam">
<form action="#CGI.SCRIPT_NAME#" method="post">
<select>
<option value="#ID#">#teamname#</option>
</select>
<input type="hidden" name="ID" value="#deleteteam.ID#">
<input type="submit" name="delete_button" value="delete"/>
</form>
</cfoutput>
</cfif>
</body>
</html>
<cfoutput query="deleteteam">
<form action="#CGI.SCRIPT_NAME#" method="post">
<select>
<option value="#ID#">#teamname#</option>
</select>
<input type="hidden" name="ID" value="#deleteteam.ID#">
<input type="submit" name="delete_button" value="delete"/>
</form>
</cfoutput>
This is where you are going wrong. If you think of outputting a query as performing a loop. each time you loop over the query you are making another form with one select option in it.
You should change your code to look something like this.
<form action="#CGI.SCRIPT_NAME#" method="post">
<select>
<cfoutput query="deleteteam">
<option value="#deleteteam.ID#">#deleteteam.teamname#</option>
</cfoutput>
</select>
<input type="hidden" name="ID" value="#deleteteam.ID#">
<input type="submit" name="delete_button" value="delete"/>
</form>
What My code is doing is just adding options for each query item not the complete from.
Hope that makes sense.

Accessing variables of a dynamic form

I am creating a form with cfloop and need to access each variable individually when submitting the form. I need to use the selected entries of the form in a loop that will add my selections to a database.
This is my form:
<form method="post">
<input type="hidden" name="isPost" value="1">
...
<cfoutput>
<cfloop query="client_admin_surveys">
<input type="text" size="35" name="surveyID" id="surveyID" value="#id#">
<input type="text" size="35" name="surveyName" id="surveyName" value="#name#">
<input type="checkbox" name="amplify" id="amplify">
<input type="checkbox" name="enchance" id="enchance">
<input type="checkbox" name="pacify" id="pacify">
<input type="checkbox" name="pacifyUrgent" id="pacifyUrgent">
</cfloop>
</cfoutput>
...
<input type="submit" name="submit" value="Submit">
</form>
After posting the form, the results group all of my selections because I have the same "name" for my form elements. I tried adding an i count next to each name to make it different but then I got a bit confused about how to process the fields.
You started down the correct path when you added the counter - go back and add that, something like:
<input type="checkbox" name="amplify#client_admin_surveys.currentRow#" id="amplify">
Would work.
I also sometimes like to add a form field for the 'counter' on the processing page
<input type="hidden" name="counter" value="#client_admin_surveys.recordCount#" />
Then on the processing page, you can loop over the counter and access the form fields using bracket notation
<cfloop from="1" to="#form.counter#" indexd="i">
<cfset thisAmplify = form["amplify" & i] />
<cfset thisEnhance = form["enhance" & i] />
<!---- more logic here --->
</cfloop>

Preserving input value of type="file" in ColdFusion on postback

I have a form in ColdFusion that initially has 5 input fields for file uploading. Should the user realize they have more than 5 files to upload in the process of attaching them, I would like the form to preserve the values when it submits itself for the change in # of fields.
Using the <cfform> tag with the preservedata="yes" attribute is supposed to accomplish this - but all I'm getting is a temp value stored in the input's value on resubmit that isn't displayed in the field nor works for a submission.
edit: Thanks for the great answers everyone, you all helped and were correct. I was able to implement Adam's suggested solution. Works great! Thanks!
function changeFieldCount() // javascript function for submit on input count change
{
var count = document.forms[0].numtotal.options[document.forms[0].numtotal.selectedIndex].value;
document.forms[0].action = "me.cfm?count=" + count;
document.forms[0].submit();
}
<cfparam name="url.count" default="5">
<cfform name="dispfiles" method="post" enctype="multipart/form-data" preservedata="yes">
<!--- looping for file input fields --->
<cfloop index="counter" from="1" to="#url.count#">
<cfinput type="file" name="file#counter#" size="50" maxlength="60"><br>
</cfloop>
<br>Number of Files to Attach:
<!--- looping for # of input selection--->
<cfselect name="numtotal">
<cfloop index="cnt" from="1" to="20" step="1">
<cfoutput>
<option value="#cnt#" <cfif #cnt# eq #url.count#>selected</cfif>>
#cnt#
</option>
</cfoutput>
</cfloop>
</cfselect>
<cfinput type="button" name="op-display" value="Display" onclick="changeFieldCount()">
<cfinput type="button" name="op-upload" value="Attach Files" onclick="submitfrm(this.form)">
<cfinput type="button" name="cancel" value=" Cancel " onclick="window.close()">
</cfform>
This is what I'm getting when I view source on the resulting submission:
<input name="file1" id="file1" type="file" value=".../cfusion.war/neotmp8858718543274653167.tmp" maxlength="60" size="50" /><br>
This is by design in all browsers for security reasons. There is no way you can insert the value for a file field.
To elaborate on #SpliFF's answer, what you need to do is dynamically create more file fields with JavaScript.
Here's an example that uses jQuery to make the JavaScript a little easier. I've also used a variable to track the number of file fields displayed so that they all have a unique number appended to their names, making it possible to loop over and uniquely identify them server-side.
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
//track the current number of file fields displayed
var numUploadFields = 5;
//attach an anonymous function to the button to add more fields
$(function(){
$("#add5").click(function(){
for (var i = 0; i < 5; i++){
numUploadFields += 1;
var newHTML = "<br/><input type='file' name='upload" +
numUploadFields + "' />";
$("#someSection").append(newHTML);
}
});
});
</script>
<form method="post" action="">
<div id="someSection">
<input type="file" name="upload1" /><br/>
<input type="file" name="upload2" /><br/>
<input type="file" name="upload3" /><br/>
<input type="file" name="upload4" /><br/>
<input type="file" name="upload5" />
</div>
<input type="button" id="add5" value="Add 5 more file fields" />
</form>
Build additional file inputs using JS DOM methods. Since you aren't leaving the page this is fast and nothing is lost.