ColdFusion - Using a cfloop with multiple fields and multiple submit buttons - coldfusion

I'm looking to pass form values in a cfform to a PDF using cfpdfform. Here's my little test page that loops through 50 records to pull the first and last name. I'm trying to just pull those into the pdf fields. Currently it puts in all 50 of the first names into the firstname field and all of the lastnames into the lastname field of the pdf. I'm not married to the submit button, but what are better options?
In my final iteration of this I'll be pulling in about 100 fields.
--Form--
<cfform name="autopdf" method="POST" action="automated_pdf_submit.cfm" enctype="multipart/form-data">
<h1>Select a state to insert into a PDF form</h1>
<div class="center">
<select name="pdfselect" id="pdfselect">
<option value="" selected>--Select State--</option>
<option value="FROI_NY.pdf">New York</option>
<option value="FROI_PA.pdf">Pennsylvania</option>
</select>
<cfinput type="hidden" name="statevalidate" onValidate="yourFunction"
message="YOU MUST SELECT A STATE TO CONTINUE!">
</div>
<table align="center" style="width:400px">
<tr>
<th></th>
<th>First Name</th>
<th>Last Name</th>
<th>Export to PDF</th>
</tr>
<cfoutput>
<cfloop query="#qryPersons#" startrow="1" endrow="50" >
<tr class="#IIf(CurrentRow Mod 2, DE('rowOdd'), DE('rowEven'))#" onmouseover="this.className='rowHighlight'"
<cfif CurrentRow Mod 2>onmouseout="this.className='rowOdd'"
<cfelse>onmouseout="this.className='rowEven'"</cfif>>
<td>#qryPersons.CurrentRow#</td>
<td>#qryPersons.LastName#</td>
<input type="hidden" name="FirstName" value="#qryPersons.LastName#">
<td>#qryPersons.FirstName#</td>
<input type="hidden" name="LastName" value="#qryPersons.FirstName#">
<td style="width:50px"><input type="submit" value="Create PDF"</td>
</tr>
</cfloop>
</cfoutput>
</table>
</cfform>
--Action--
<cfpdfform action="populate" source="forms\#form.pdfselect#">
<cfpdfformparam name="FirstName" value="#form.FirstName#">
<cfpdfformparam name="LastName" value="#form.LastName#">
</cfpdfform>

Your form fields are all named FirstName and LastName you need to make those unique
<cfloop query="#qryPersons#" startrow="1" endrow="50" >
<tr class="#IIf(CurrentRow Mod 2, DE('rowOdd'), DE('rowEven'))#" onmouseover="this.className='rowHighlight'"
<cfif CurrentRow Mod 2>onmouseout="this.className='rowOdd'"
<cfelse>onmouseout="this.className='rowEven'"</cfif>>
<td>#qryPersons.CurrentRow#</td>
<td>#qryPersons.LastName#</td>
<input type="hidden" name="FirstName#qryPersons.currentrow#" value="#qryPersons.LastName#">
<td>#qryPersons.FirstName#</td>
<input type="hidden" name="LastName#qryPersons.currentrow#" value="#qryPersons.FirstName#">
<td style="width:50px"><input type="submit" value="Create PDF"</td>
</tr>
</cfloop>
I've never used cfpdfform before, but this syntax should work. You may need to dynamically name the name attribute below as well
<cfpdfform action="populate" source="forms\#form.pdfselect#">
<cfloop from="1" to="50" index="i">
<cfpdfformparam name="FirstName" value="#form['FirstName'&i]#">
<cfpdfformparam name="LastName" value="#form['LastName'&i]#">
</cfloop>
</cfpdfform>

Related

Error executing query, using ajax?

I'm updating a query using a form and ajax.
I added a new column (select option) to the form, it work before adding the select option.
I'm trying to get the value of the #dept_id# but i keep getting error
'NetworkError: 500 Error Executing Database Query'.
This has to do with the update query I have in delete_record.cfc.
when adding dept=#selectDept# to the query is when i get the error.
Taking this part off the query works and it updates.
Below its the form and after that its the delete_record.cfc file.
I also made a http://jsfiddle.net/6focvy54/4/
where I poster the ajax call and have the delete_record.cfc file code.
<form method="post" action="" id="confrm_key">
<table >
<tr>
<th >Add?</th>
<th >Delete?</th>
</tr>
<cfoutput query="total">
<tr id="hd#emp_id#">
<td>
<select name="selectDept" >
<option selected="selected" >Select Department</option>
<cfloop query="department">
<option value="#dept_id#">#dept_name#</option>
</cfloop>
</select>
</td>
<td> <input type="checkbox" name="check_delete" value="#emp_id#" >Yes</td>
</tr>
</cfoutput>
</table>
<p><input type="button" name="Submit" value="Remove" onclick="sub_keys();"></p>
</form>

How to make date and time form values insert into one column?

I have a form where the user selects a date and time. The user's selections represent the time and date they want specific answers, which they enter in the same form, to display. Then I grab the selected values and insert them into a table: cse_result_summary. Right now my code is inserting them correctly.
I'm using this jquery plugin for the date. Time is just a simple select. Something I did not think about before I finish the form was how I would compare today's date to the show_date.
The code below will work for the show_date, but the problem is with the time. Even if the date is greater, but the time is not, it will have to wait for that time to display. I would like it to display at that specific time and date. When its after that date and time it should no longer matter the time it is.
Since I'm inserting the date and time in different columns, I'm wondering if its possible to insert them into one column? That way I will only have to compare todayDate > formDate:
<cfset dtToday = Now() />
<cfif DateFormat(dtToday, "yyyy--mm--dd") gte DateFormat(getdates.show_date)
&& TimeFormat(dtToday) gte timeformat(getdates.show_time)>
different file (form file)
<tr>
<td> Date it will display (Please enter date format mm/dd/yyyy.):</td>
<td><input class="inputDate" id="inputDate"
value="07/01/2014" NAME="date_used"/>
</td>
</tr>
<tr>
<td> Time it will display (Please enter time format hh:mm tt):
<td><select ID="time_used" NAME="time_used" VALIDATE="date" >
<option selected> Select Time</option>
<option value="7:00 AM">7:00 AM</option>
<option value="7:15 AM">7:15 AM</option>
<option value="7:30 AM">7:30 AM</option>
<option value="7:45 AM">7:45 AM</option>
<option value="8:00 AM">8:00 AM</option>
</select></TD>
</td>
</tr>
</tbody>
</table>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
<cfif FormSubmit eq "FormSubmitted">
... more code....
<cfset month_date_show = #DateFormat(Trim(form.month_date_show), "mm-15-yyyy")#>
<cfset newdate = #DateFormat(Trim(date_used), "mm-dd-yyyy")# />
<cfset time_used = #TimeFormat(Trim(time_used),"h:mm tt")#>
<cfquery datasource="Intranet" name="InsertRequest">
INSERT INTO cse_result_summary
( show_date, show_time,monthly_enter_date,monthly_past_date )
VALUES
( '#newdate#','#time_used#',getdate(),'#month_date_show#' )
</cfquery>
Assuming your database field type is date/time you could do something like this:
<tr>
<td> Date it will display (Please enter date format mm/dd/yyyy.):</td>
<td><input class="inputDate" id="inputDate" value="07/01/2014" NAME="date_used"/>
</td>
</tr>
<tr>
<td> Time it will display (Please enter time format hh:mm tt):
<td><select ID="time_used" NAME="time_used" VALIDATE="date" >
<option selected> Select Time</option>
<option value="7:00 AM">7:00 AM</option>
<option value="7:15 AM">7:15 AM</option>
<option value="7:30 AM">7:30 AM</option>
<option value="7:45 AM">7:45 AM</option>
<option value="8:00 AM">8:00 AM</option>
</select></TD>
</td>
</tr>
</tbody>
</table>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
<cfif FormSubmit eq "FormSubmitted">
... more code....
<cfset month_date_show = #DateFormat(Trim(form.month_date_show), "mm-15-yyyy")#>
<cfset newdate = #DateFormat(Trim(date_used), "mm-dd-yyyy")# />
<cfset time_used = #TimeFormat(Trim(time_used),"h:mm tt")#>
<Cfset combinedDateTime="#newdate #time_used#">
<cfquery datasource="Intranet" name="InsertRequest">
INSERT INTO cse_result_summary (show_date, show_time,monthly_enter_date,monthly_past_date, *yourcolumname*)
VALUES ('#newdate#','#time_used#',getdate(),'#month_date_show#', <cfqueryparam cfsqltype="cf_sql_timestamp" value="#combinedDateTime#">)
</cfquery>

CFINPUT required="yes" not working with CFLOOP

When I click Submit button (no data was entered) the text boxes were empty and it still goes to the next page without popping up any messsage. It seems the cfinput required="yes" does not work the cfloop. I thought it should work even with the cfloop. I could not find what was wrong. How can I make it work? any info is greatly appreciated. Thank you.
<cfform name="theForm" action="nextPage.cfm" method="post">
<table>
<tr>
<td><cfinput type="text" name="A" size="50" required="yes message="please enter your text"></td>
</tr>
<td><cfinput type="text" name="B" size="50" required="yes message="please enter your text"></td>
</tr>
<cfloop from=1 to=5 index=i>
</tr>
<td><cfinput type="text" name="C" size="50" required="yes message="please enter your text"></td>
</tr>
</tr>
<td><cfinput type="text" name="D" size="50" required="yes message="please enter your text"></td>
</tr>
</cfloop>
<tr>
<td><cfinput type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</cfform>
For this to work, you need to have unique NAMEs for each input field. Right now, you're creating a bunch of inputs named either "C" or "D". If any one "C" is filled in, then no other field named "C" needs to be filled in. Create unique names using your index variable and the validation should work as you intend.
<cfloop from=1 to=5 index=i>
</tr>
<td><cfinput type="text" name="C_#i#" size="50" required="yes message="please enter your text"></td>
</tr>
</tr>
<td><cfinput type="text" name="D_#i#" size="50" required="yes message="please enter your text"></td>
</tr>
</cfloop>
There is a syntax error in the cfinput tag
It should be
<cfinput type="text" name="D" size="50" required="yes" message="please enter your text">
There is a missing quote after yes
In addition to what was said you should use something else and not cfform if you want anything more than basic validation. The cfform stuff is really legacy and there are much better options these days.

checkbox on page load in coldfusion

I have 2 check boxes like below on a coldfusion form. On page load I want both to be checked
and display a query result depending on which checkbox is checked. I have the below code working fine except for the scernario where I uncheck both the checkboxes and hit view now. As im setting
form.chkbox=""
on page load they are remaining checked even after I uncheck them. How can i make them remain unchecked upon page load. I tried using the JS function, but it doesnt seem to work
<script type="text/javascript">
function callme(){
var box1 = document.getElementById('chkbox1').checked;
var box2 = document.getElementById('chkbox2').checked;
if (box1 && box2){
alert("checked") ;
}else{
box1.checked = false;
box2.checked = false;
}
}
</script>
<cfset form.chkbox="">
<form action="view_emp_qual.cfm?show=yes" method="post" name="Myform">
<table align="center">
<tr>
<td>
<cfif isDefined("form.chkbox") and (form.chkbox eq "" or listfind(form.chkbox, 1))>
<input type="checkbox" checked="checked" name="chkbox" id="chkbox1" value="1">
<cfelse>
<input type="checkbox" name="chkbox" id="chkbox1" value="1">
<input type="hidden" name="chkbox" id="chkbox1" value="1">
</cfif>
<strong> Agreement Only</strong>
<cfif isDefineD("form.chkbox") and (form.chkbox eq "" or listfind(form.chkbox, 2))>
<input type="checkbox" checked="checked" name="chkbox" id="chkbox2" value="2">
<cfelse>
<input type="checkbox" name="chkbox" id="chkbox2" value="2">
<input type="hidden" name="chkbox" id="chkbox2" value="2">
</cfif>
<strong>Active Employees</strong>
</td>
<td><input type="Submit" name="submitnow" value="View Selected" class="button" onclick="return callme();"> </td>
</tr>
</table>
</form>
<cfif not isDefined("form.chkbox")>
Query1
<cfelseif isDefined("form.chkbox") and ( listfind(form.chkbox, 1) eq 0 and listfind(form.chkbox, 2) eq 1)>
Query 2
<cfelseif isDefineD("form.chkbox") and (listfind(form.chkbox, 1) eq 1 and listfind(form.chkbox, 2) eq 0)>
query 3
<cfelse>
query4
</cfif>
If you are submitting the form in same page then following code may solve your purpose.
I have made some adjustment with your main code. Please have a look.
<cfdump var="#form#" label="before">
<!---flag for checking if form has submitted or not--->
<cfif Not isDefined("form.submitnow")>
<cfset form.chkbox = "1,2">
</cfif>
<cfdump var="#form#" label="after">
<cfif isDefined("form.chkbox") >
<cfdump var="#listfind(form.chkbox, 1)#" ><br>
<cfdump var="#listfind(form.chkbox, 2)#" >
</cfif>
<form action="" method="post" name="Myform">
<table align="center">
<tr>
<td>
<!---I have removed hidden fields and made a few changes in conditional checking --->
<cfif isDefined("form.chkbox") and (listfind(form.chkbox, 1))>
<input type="checkbox" checked="checked" name="chkbox" id="chkbox1" value="1"><span>1</span>
<cfelse>
<input type="checkbox" name="chkbox" id="chkbox1" value="1"><span>2</span>
</cfif>
<strong> Agreement Only</strong>
<cfif isDefineD("form.chkbox") and (listfind(form.chkbox, 2))>
<input type="checkbox" checked="checked" name="chkbox" id="chkbox2" value="2"><span>3</span>
<cfelse>
<input type="checkbox" name="chkbox" id="chkbox2" value="2"><span>4</span>
</cfif>
<strong>Active Employees</strong>
</td>
<td>
<input type="Submit" name="submitnow" value="View Selected" class="button">
</td>
</tr>
</table>
</form>
<cfif not isDefined("form.chkbox")>
Query1
<cfelseif isDefined("form.chkbox") and ( listfind(form.chkbox, 1) eq 0 and listfind(form.chkbox, 2) eq 1)>
Query 2
<cfelseif isDefineD("form.chkbox") and (listfind(form.chkbox, 1) eq 1 and listfind(form.chkbox, 2) eq 0)>
query 3
<cfelse>
query4
</cfif>

ColdFusion 9 ReReplace nth occurence of HTML tag

I have the following HTML stored in a variable in ColdFusion 9. I need to insert a new table row after the 4th </tr>. i.e. before the Submit button.
<form name="form1" id="form1" action="" method="post">
<table>
<tr style="visibility:hidden;display:none;"><td> <input type="hidden" id="ref1" name="ref1" value="1" > </td></tr>
<tr style="visibility:hidden;display:none;"><td> <input type="hidden" id="ref2" name="ref2" value="2" > </td></tr>
<tr>
<th style="text-align:left;">Name * </th>
<td><input type="text" name="foo" id="foo" size="30" maxlength="50" value=""></td>
</tr>
<tr>
<th title="Please enter plain text or HTML." style="cursor:help;text-align:left;">Comment * </th>
<td><textarea name="bar" id="bar" cols="40" rows="10" ></textarea></td>
</tr>
<tr>
<th colspan="1"></th>
<td>
<input style="width:80px" type="submit" value="Submit">
<input style="width:80px" type="button" value="Cancel">
</td>
</tr>
</table>
ReReplace seems like the way to go, but I'm having trouble getting the regexp right. Another option would be to split the string and rebuild it with my new HTML in the middle. Any suggestions would be appreciated.
Regex is the wrong tool for this - you want a HTML parser.
Here's how you can do it with JSoup:
<cfsavecontent variable="InputHtml">
[insert code from question]
</cfsavecontent>
<cfsavecontent variable="NewRow">
<tr><th>whatever</th><td>stuff</td></tr>
</cfsavecontent>
<!--- Read "Creating Objects From Jar Files" --->
<cfset jsoup = createObject('java','org.jsoup.Jsoup') />
<cfset HtmlDom = jsoup.parse(InputHtml) />
<cfset HtmlDom.select('tr:eq(4)').after( NewRow ) />
<cfoutput>
<pre>#XmlFormat(HtmlDom.body().html())#</pre>
</cfoutput>
You can see details of what selectors are supported in the JSoup Selector API
If you don't know/care how many lines are in the table, you can do...
HtmlDom.select('table>tbody').append( NewRow )
...to just add the new row at the end.
Creating Objects From Jar Files
The above code most likely wont work instantly if you copy and paste it, because your server doesn't know about JSoup - you need to download the Jar file and put it in a sensible location.
For CF9, you need to copy the jsoup-1.6.3.jar into your {coldfusion}/lib directory then restart the server.
For CF10, you can use this.JavaSettings in your Application.cfc (as described here) to place it in a different location.
For Railo and OpenBD, you can specify the location of the JAR file as a third argument, e.g:
<cfset jsoup = createObject('java','org.jsoup.Jsoup','lib/jsoup-1.6.3.jar') />
I recommend doing this with jQuery:
​$(document).ready(function(){
$($('form tr')[3]).after('<tr><td>row</tr></tr>');
});​
Much easier.