Passing form input using Coldfusion to external URL - coldfusion

I am trying to pass a form input to an external URL to be used somewhere else and perform a certain task. The issue I am having is actually getting the value and passing it. I either get a LotNumber is undefined or an error executing a Cfquery. I tried using CFPARAMhowever I think I misunderstand what cfparam does. Hopefully this is something simple.
Here is some code:
<table border="1" cellpadding="3" cellspacing="0" rules="GROUPS" frame="BOX" width="100%" bordercolor="#C0C0C0" bgcolor="#FFFFFF">
<thead>
<tr height="28">
<td colspan="4"><b>Outstanding Shipping</b></td>
<CFOUTPUT>
<cfparam name="Show_SampleLogSheet.Passed_LotNumber" default="" />
<td align="left" colspan="1">
<input class="frm3" type="text" id="Outstanding_Passed_LotNumber" size="3" maxlength="6" tabindex="25" style="background-color: ##838383;border:1px solid ##000000; color:white">
<form name="Show_SampleLogSheet" class="frm" action="/Buying/Shipping_Advice/Index.cfm?Passed_CustomerID=#Passed_CustomerID#&Passed_ShippingAdviceID=#Get_ShippingAdvice.ShippingAdviceID#&Passed_Lot_Number=#Show_SampleLogSheet.Passed_LotNumber#&Passed_Activate=1" method="post" style="display: inline">
<input type="hidden" name="Passed_CustomerID" value="#Passed_CustomerID#">
<input class="frm3" type="text" name="Passed_LotNumber" value="#Show_SampleLogSheet.Passed_LotNumber#" size="3" maxlength="6" tabindex="25">
</form>
</td>
</CFOUTPUT>
I really appreciate any help.
Thank you

For names are client side. ColdFusion does not need to name them at all. (Code has been somewhat similified
<cfparam name="Passed_LotNumber" default="" />
I don't know what this field is good for. It is not within the form tag, so it is not going to get pushed over on submit.
<input class="frm3" type="text" id="Outstanding_Passed_LotNumber" size="3" maxlength="6" tabindex="25" style="background-color: ##838383;border:1px solid ##000000; color:white">
Real form starts here. Note that passed_LotNumber does not need anything
<form name="Show_SampleLogSheet" class="frm" action="/Buying/Shipping_Advice/Index.cfm?Passed_CustomerID=#Passed_CustomerID#&Passed_ShippingAdviceID=#Get_ShippingAdvice.ShippingAdviceID#&Passed_Lot_Number=#Passed_LotNumber#&Passed_Activate=1" method="post" style="display: inline">
<input type="hidden" name="Passed_CustomerID" value="#Passed_CustomerID#">
<input class="frm3" type="text" name="Passed_LotNumber" value="#Passed_LotNumber#" size="3" maxlength="6" tabindex="25">

Turns out it was some the wrong input name. Here is the fixed code:
<td align="left" colspan="1">
<input class="frm3" type="text" id="Outstanding_Passed_LotNumber" size="3" maxlength="6" tabindex="25" style="background-color: ##838383;border:1px solid ##000000; color:white">
<form name="Show_SampleLogSheet" class="frm" action="/Buying/Shipping_Advice/Index.cfm" method="post" style="display: inline">
<input type="hidden" name="Passed_CustomerID" value="#Passed_CustomerID#">
<input class="frm3" type="text" name="Passed_Lot_Number" size="3" maxlength="6" tabindex="25">
</form>
</td>
There was a parameter that was hidden somewhere else named Passed_Lot_Number instead of Passed_LotNumber. I apologize, this is some super crap code and it's super old thus all of these dumb headaches. Thank you everyone

Related

extract content from html snippet

I have the following code snippet and I'm looking for a better way to parse out the last name.
<TABLE BORDER="0" class="info" width="560">
<TR>
<TD VALIGN="top"> <B>First Name<B></FONT> </TD>
<TD VALIGN="top"> <INPUT TYPE="text" NAME="First_Name" SIZE="16" value="Ashley"> </TD>
<TD VALIGN="top"> <B>Last Name<B></FONT> </TD>
<TD VALIGN="top"> <INPUT TYPE="text" NAME="Last_Name" SIZE="16" value="Smith"> </TD>
</TR>
<tr>
<TD VALIGN="top" colspan="2"> <B>Company Name (if any):<B></FONT> </TD>
<TD VALIGN="top" colspan="2"> <INPUT TYPE="text" NAME="Company_Name" SIZE="24" value=""> </TD>
</tr>
<TR>
<TD VALIGN="top" colspan=2> <B>Address<B></FONT> </TD>
<TD VALIGN="top" colspan=2> <INPUT TYPE="text" NAME="Address" SIZE="24" value="123 Any Street Circle "> </TD>
</TR>
<tr>
<TD VALIGN="top" colspan=2> <B>City <B></FONT> <INPUT type="text" id="City" name="City" SIZE="14" value="Shady Town"> </TD>
<TD colspan="2" VALIGN="top"> <B>State<B></FONT> <INPUT type="text" id=State name=State SIZE="4" value="Tx"> <B>Zip<B></FONT> <INPUT type="text" id=Zip name=Zip SIZE="8
I have the following but I'm pretty sure I can do this without having to do the replace. What I'm trying to do below is find the starting point, finding the end point, and then taking the text in between. Then once I have that, remove the "matched" text leaving me with the value of the input field.
<cfset LastName_start = findNoCase('<INPUT TYPE="text" NAME="Last_Name" SIZE="16" value="', theString, 0)>
#lastName_start#
<cfset LastName_end = findNoCase('">', theString, 0)> #lastName_end#
<cfset lastNameValue = '#Mid(theString,LastName_start,LastName_end)#'>
#lastNameValue#
<cfset lastNameValue = replace(lastNameValue, '<INPUT TYPE="text" NAME="Last_Name" SIZE="16" value="', '')>
<cfset lastNameValue = replace(lastNameValue, '">', '')>
<cfset lastNameValue = listFirst(lastNameValue,'"')>
<cfdump var="#lastNameValue#" label="lastNameValue">
Any tips on how I can clean this up using ColdFusion? This is an ethical exercise.
And yes, I did try to format this.
I second Scott Stroz's suggestion about trying JSoup. It usually works well, and is very simple to use.
Download the JSoup jar and load it in your Application.cfc.
component {
this.name = "MyApplication";
this.javaSettings = { loadPaths = ["C:\path\to\jsoup-1.12.1.jar"] };
// ... more application settings
}
Create an instance of JSoup, parse the HTML string and use val() to grab the text of the first matching element. It returns an empty string if the element wasn't found.
You can find a bunch of other helpful examples in the JSoup Cookbook.
<cfscript>
yourHTMLString = '<TABLE BORDER="0" class="info" ......';
// parse html
jsoup = createObject("java", "org.jsoup.Jsoup");
root = jsoup.parse( yourHTMLString );
// get the first matching value ...
lastName = root.select("input[name='Last_Name']").val();
firstName = root.select("input[name='First_Name']").val();
companyName = root.select("input[name='Company_Name']").val();
cityName = root.select("input[name='City']").val();
stateName = root.select("input[name='State']").val();
address = root.select("input[name='Address']").val();
</cfscript>
Results:

jstl if/else doesn't work correctly

I want my code to do this...
<c:if test="${validated != 'Y' }">
<form:form id="newJob" method="post" action="updateValidated" commandName="jobModel">
<form:input path="regattaId" type="hidden" />
<form:input path="job_id" type="hidden" />
<table>
<tr>
<td><spring:message code="label.validated" />
<td><form:radiobutton path="validated" value="Y" />Yes<form:radiobutton
path="validated" value="N" />No</td>
</tr>
</table>
<input type="submit" class="button" value='<spring:message code="label.validateJob"/>' hidden="true" />
</form:form>
</c:if>
<c:else>
This job is validated
</c:else>
Any takers? Right now it doesn't work.
I think my syntax is correct but I'm very new
<c:else> doesn't exist, and has never existed. Read the documentation of what you're using instead of trying random things.
The correct way, using the JSTL, to have the equivalent of if/else is
<c:choose>
<c:when test="condition">...</c:when>
<c:otherwise>...</c:otherwise>
</c:choose>
the equivalent code is
<c:choose>
<c:when test="${validated != 'Y' }">
<form:form id="newJob" method="post" action="updateValidated"
commandName="jobModel">
<form:input path="regattaId" type="hidden" />
<form:input path="job_id" type="hidden" />
<table>
<tr>
<td><spring:message code="label.validated" />
<td><form:radiobutton path="validated" value="Y" />Yes<form:radiobutton
path="validated" value="N" />No</td>
</tr>
</table>
<input type="submit" class="button"
value='<spring:message code="label.validateJob"/>' hidden="true" />
</form:form>
</c:when>
<c:otherwise>
This job is validated
</c:otherwise>
</c:choose>
Jstl's c:if tag doesn't have a companion c:else tag. Try c:choose!

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

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>

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.

Django formset apparently only printing first form in template (because of invalid markup)

[Edit: See my answer below - the origin of this issue is invalid markup, and browsers working very hard to hide that. ]
I have a formset which definitely should contain two forms, but for whatever reason, I am only getting one form printed in the template.
This is the template line:
<tr id="existing_docs_row"><td colspan="2">{{ existing_articles.management_form }}{% for f in existing_articles %}<div>{{ f }}</div>{% endfor %}</td></tr>
I get the exact same behaviour (less div tags) with:
<tr id="existing_docs_row"><td colspan="2">{{ existing_articles }}}</td></tr>
The management form and first form are created, but not the second. This is what I get in my browser:
<input type="hidden" id="id_form-TOTAL_FORMS" value="2" name="form-TOTAL_FORMS"><input type="hidden" id="id_form-INITIAL_FORMS" value="2" name="form-INITIAL_FORMS"><input type="hidden" id="id_form-MAX_NUM_FORMS" name="form-MAX_NUM_FORMS"><div><div class="selected_row " id="selected_row"><span class="formlabel"></span><ul>
<li><label for="id_form-0-selected_0"><input type="radio" name="form-0-selected" value="True" id="id_form-0-selected_0"> </label></li>
</ul></div>
<div class="original_filename_row " id="original_filename_row"><span class="formlabel"><span id="for-id_form-0-original_filename-">Original filename:</span></span><div id="id_form-0-original_filename" name="form-0-original_filename">FakeExampleCompanyName.docx</div></div>
<div class="tags_row " id="tags_row"><span class="formlabel"><span id="for-id_form-0-tags-">Tags:</span></span><div id="id_form-0-tags" name="form-0-tags" class="tagarea"><span class="tagitem">England and Wales</span> <span class="tagitem">Private company limited by shares</span> <span class="tagitem">Model articles with amendments</span></div></div>
Breaking in the view, and printing the formset shows that it contains two forms (existing_template_formset is the name of the formset inside the view):
>>> print existing_template_formset <input type="hidden" name="form-TOTAL_FORMS" value="2" id="id_form-TOTAL_FORMS" /><input type="hidden" name="form-INITIAL_FORMS" value="2" id="id_form-INITIAL_FORMS" /><input type="hidden" name="form-MAX_NUM_FORMS" id="id_form-MAX_NUM_FORMS" />
<div id="selected_row" class="selected_row "> <span class="formlabel"></span><ul> <li><label for="id_form-0-selected_0"><input type="radio" id="id_form-0-selected_0" value="True" name="form-0-selected" /> </label></li> </ul></div> <div id="original_filename_row" class="original_filename_row "><span class="formlabel"><span id="for-id_form-0-original_filename-">Original filename:</span></span><div name="form-0-original_filename" id="id_form-0-original_filename">FakeExampleCompanyName.docx</div></div> <div id="tags_row" class="tags_row "><span class="formlabel"><span id="for-id_form-0-tags-">Tags:</span></span><div class="tagarea" name="form-0-tags" id="id_form-0-tags" ><span class="tagitem" >England and Wales</span> <span class="tagitem" >Private company limited by shares</span> <span class="tagitem" >Model articles with amendments</span></div></div> <tr><th></th><td><input type="hidden" name="form-0-id" id="id_form-0-id" /></td></tr>
<div id="selected_row" class="selected_row "><span class="formlabel"></span><ul> <li><label for="id_form-1-selected_0"><input type="radio" id="id_form-1-selected_0" value="True" name="form-1-selected" /> </label></li> </ul></div> <div id="original_filename_row" class="original_filename_row "><span class="formlabel"><span id="for-id_form-1-original_filename-">Original filename:</span></span><div name="form-1-original_filename" id="id_form-1-original_filename" >FakeExampleCompanyName.docx</div></div> <div id="tags_row" class="tags_row "><span class="formlabel"><span id="for-id_form-1-tags-">Tags:</span></span><div class="tagarea" name="form-1-tags" id="id_form-1-tags" ></div></div> <tr><th></th><td><input type="hidden" name="form-1-id" id="id_form-1-id" /></td></tr>
>>> len(existing_template_formset) 2
As you can see, in both cases, the total number of forms in the formset is 2 (as evidenced in the management form), but the second one is simply not generated.
Has anyone come across this before? How do I fix this?
I'm using django 1.3.1 on python 2.7.2 on windows.
For completeness, here is the code which creates the formset:
class ExistingTemplateFormset(modelformset_factory(ArticlesTemplate, extra = 0, form=ExistingTemplateForm)):
def __init__(self, *args, **kwargs):
super(ExistingTemplateFormset, self).__init__(*args, **kwargs)
for x in self:
x.fields['id'].widget = forms.HiddenInput()
x.fields['original_filename'].editable = False
x.fields['original_filename'].widget = SpanWidget(tag = u'div')
x.fields['tags'].widget= TagArea()
x.fields['tags'].help_text = u''
(TagArea and SpanWidget exist)
In the view:
existing_template_formset = ExistingTemplateFormset(queryset = the_organisation.get_template_articles())
Sharp-eyed readers (which, it turns out, does not include me, hence this problem) will note that my output includes at the end of each form:
`<tr><th></th><td><input type="hidden" name="form-0-id" id="id_form-0-id" /></td></tr>`
Now, when that is substituted into <tr id="existing_docs_row"><td colspan="2">{{ existing_articles.management_form }}{% for f in existing_articles %}<div>{{ f }}</div>{% endfor %}</td></tr> that leads to invalid markup (a tr inside a tr!).
So, it turns out that the template was generating the second form, but the browser's error recovery methods (in chrome, disregarding a lot of the invalid markup; in firefox, floating the second form to elsewhere in the DOM) created the appearance that the second form wasn't being generated.
To summarise: just examining the DOM mislead me. Try to force your browser to choke on errors, and look at the raw markup.