Coldfusion - Update form - forms

Can you advice me please? how to make working form which will allow you insert and update items?
I have one form which works for inserting but in moment when I want to update <input> in this form, it returning error with all data which are in form. So I want to ask you for help with updating.
My code:
Page 1
Insert part (this works)
<cfif isdefined("Form.Name")>
<cfquery name"something" datasource="#something#>
INSERT INTO table..
Values (...)
</cfquery>
</cfif>
Update part
<cfif isDefined('url.action')>
<cfquery datasource="Something" name="Something">
Update table..
SET Name= <cfqueryparam cfsqltype="cf_sql_varchar" value="#Form.Name#">
Where Some_id = '#Form.ID#'
</cfquery>
</cfif>
Page1 Form
<table>
<form action="path\Page1.cfm?action=update" method="post">
<cfloop query="languageField">
<input name="Name" value="#SomeQuery.Name#">
<input type="Submit" value="Submit">
</cfloop>
</form>
</table>

Your update statement has a missing # after the form.id variable. You also have single quotes around the value of the form.id which will pass in the number as a string. You should also wrap all user generated data with cfqueryparam tags to protect against SQL Injection attacks.
Where Some_id = '#Form.ID'
should be
Where Some_id = #Form.ID#
but should more properly be
Where Some_id = <cfqueryparam value="#Form.ID#" cfsqltype="cf_sql_bigint">

Okay, I have answer.
Query for updating must look like
<cfloop list="#form.id#" item="id">
<cfquery datasource="Something" name="updateform">
UPDATE table
SET
column_name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form['Edit_Name_' & id]#" />
WHERE id = #id#
</cfquery>
</cfloop>
And into form:
<input type="hidden" name="Name" value="#query.Name#">
<tr>
<td><input name="Edit_Name_#Name#" type="text" value="#query.Name#"></td>
</tr>

Related

How to ignore null values in mongodb insert query

I am trying to insert some data into mongodb database from a simple registration form. It includes first name, middle name and last name in which middle name is optional. Now I want insert middle name into database only if user provides it in the form.
fname = request.values.get('fname')
mname = request.values.get('mname')
lname = request.values.get('lname')
db.users.insert({'fname':fname, 'mname':mname, 'lname':lname})
I am getting this output:
id:5d11c378f829d8938bf65e0b
fname:"test"
mname:""
lname:"user"
I want this( if mname is not provided in the form):
id:5d11c378f829d8938bf65e0b
fname:"test"
lname:"user"
<form action="/register" method="post">
First name:<br>
<input type="text" name="fname" required>
<br>
Middle name:<br>
<input type="text" name="mname"> <!-- optional -->
<br>
Last name:<br>
<input type="text" name="lname" required>
<br><br>
<input type="submit" value="Submit">
</form>
return "Registered successfully!"
How about check the empty value before insert?
var m = request.values.get(‘mname’);
mname = (m) ? m : null;
Or you can try insert object.
user.fname = request.values.get('fname')
var m = request.values.get(‘mname’)
user.lname = request.values.get('lname')
if (m){
user.mname = m
}
db.users.insert(user)

Coldfusion: specific action after selecting an option in select

I have following code:
<cfif session.language is ("DE")>
<cfset bl=ValueList(getContent.G,",")>
<cfelseif session.language is ("FR")>
<cfset bl=ValueList(getContent.H,",")>
<cfelseif session.language is ("EN")>
<cfset bl=ValueList(getContent.I,",")>
</cfif>
<cfset tags = sizes />
<cfset bltags = bl />
<cfset tagArray = arrayNew(1) />
<cfset tagArrayDATA = arrayNew(1) />
<cfloop list="#tags#" index="tag" delimiters=",">
<cfif not ArrayFindNoCase(tagArray,tag)>
<cfset arrayAppend(tagArray, tag) />
</cfif>
</cfloop>
<cfloop list="#bltags#" index="tag" delimiters=",">
<cfif not ArrayFindNoCase(tagArrayDATA,tag)>
<cfset arrayAppend(tagArrayDATA, tag) />
</cfif>
</cfloop>
<cfoutput>
<cfif isdefined("tagArray") AND arraylen(tagArray) GT 1>
<form name="frmsize" id="frmsize" action="/index.cfm?showusage" method="post">
<cfif isdefined("tagArray") AND arraylen(tagArray) GT 1>
<div>
<select name="valuesize">
<option value="">Choose your option</option>
<cfloop from="1" to="#arraylen(tagArray)#" index="i">
<option value="#tagArray[i]#">#tagArray[i]#
<cftry>
#tagArrayDATA[i]#
<cfcatch>
</cfcatch>
</cftry>
</option>
</cfloop>
</select>
</div>
</cfif>
</form>
</cfif>
</cfoutput>
My goal is to send a value from tagArrayDATA[i] via link.
It should look like that:
<form name="frmsize" id="frmsize" action="/index.cfm?showusage&valueArrayData="#tagArrayDATA[i]#" method="post">
I don't know how to manage that because the cfloop is below the action attribute of the form.
You can't really do what you are trying to do with server side code alone as far as I can tell. But you have a couple of options. One easy one is to just Javascript to update the action when the select is changed.
The other option is to put both values in the select and parse on the end.
<cfloop from="1" to="#arraylen(tagArray)#" index="i">
<option value="#tagArray[i]#-#tagArrayDATA[i]#">
#tagArray[i]# #tagArrayDATA[i]#
</option>
</cfloop>
Then when you are parsing the data just do:
<cfset data = listToArray(FORM.valuesize,'-') />
<!-- data[1] will be the selected value of #tagArray[i]# -->
<!-- data[2] will be the selected value of #tagArrayData[i]# -->
<!-- This assumes the - will never be actually in the data, you could use a different separator -->
My guess is they are both strings and this should work, though I have no idea why you have a try/catch in the select part of your code, probably look at a better way of doing that. If you really need that, I would clean it up as.
<cfloop from="1" to="#arraylen(tagArray)#" index="i">
<cfset data = '' />
<cftry>
<cfset data = tagArrayData[i] />
<cfcatch></cfcatch>
</cftry>
<option value="#tagArray[i]#-#data#">
#tagArray[i]# #data#
</option>
</cfloop>
Though if you are processing the data on the other end, I would make sure all the data is either in the FORM or the URL scopes but not mix. I would be pissed to have to parse some form data in the FORM scope and other data in the URL scope.

ColdFusion form: how to refer to a selection from a binding?

I am not sure if I am thinking about this correctly. I am still new to ColdFusion. I am creating a form which uses binding for the user to select a category and then a specific service. The code for the binding works great. The problem is that now I need to pull some additional information from the service to refer to another field. For example, if the service selected has a "Y" under additional info needed. A field will show that additional information is needed (this field is not changeable)
Originally I thought I could do something like regenerate the same query I did to pull the list of services and just refer to the service type that is selected. Yet I keep getting an error that the field is not defined. Here is the code that I am trying to make it work.
UPDATED: I got it to work I changed the code below the only problem is that I can not get the "Y" to display I get [object Object] instead. Any advice on how to display the text with use of cfinput?
CFC:
<!--- Get array of Catagory Description types --->
<cffunction name="cat_description" access="remote" returnType="array">
<!--- Define variables --->
<cfset var data="">
<cfset var result=ArrayNew(2)>
<cfset var i=0>
<!--- Get data --->
<cfquery name="getServiceCat" datasource="Some Database">
select distinct 2 AS SortBy, CATG_NAME
from some table
UNION
select 1 AS SortBy, '' AS CATG_NAME
from Some table
order by SortBy
</cfquery>
<!--- Convert results to array--->
<cfloop index="i" from="1" to="#getServiceCat.recordcount#">
<cfset result[i][1]=getServiceCat.CATG_NAME[i]>
<cfset result[i][2]=getServiceCat.CATG_NAME[i]>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
<!--- Get Service Type by Cat description type --->
<cffunction name="getServiceType2" access="remote" returnType="array">
<cfargument name="CATG_NAME" type="string" required="true">
<cfset var data="">
<cfset var result=ArrayNew(2)>
<cfset var i=0>
<cfquery name="getServiceType2" datasource="Some Database">
select 1 AS SortBy, '' AS SRTY_BNR, '' AS SRTY_NAME
from Some table
UNION
select distinct 2 AS SortBy, SRTY_NBR, SRTY_NAME
from Some Table
where CATG_NAME = <cfqueryparam value="#ARGUMENTS.CATG_NAME#"
cfsqltype="cf_sql_varchar">
order by SortBy
</cfquery>
<cfloop index="i" from="1" to="#getServiceType2.recordcount#">
<cfset result[i][1]=getServiceType2.SRTY_NAME[i]>
<cfset result[i][2]=getServiceType2.SRTY_NAME[i]>
</cfloop>
<cfreturn result>
</cffunction>
<!---Updated below table into CFC to pull information depending on what is pulled
from category and service type--->
<cffunction name="getLR" access="remote" returnType="query">
<cfargument name="SRTY_NAME" type="string" required="true">
<cfset var data="">
<!---Query to get "Y"--->
<cfquery name="lr_required" datasource="Some Datebase">
select distinct LAB_REL_NTFN_FLAG
from some table
where SRTY_NAME = <cfqueryparam value="#ARGUMENTS.SRTY_NAME#"
cfsqltype="cf_sql_varchar">
</cfquery>
<cfreturn lr_required>
</cffunction>
Here is the code within the form:
<TR id="serv_ty" style="display: inline;">
<td align="left" nowrap><label>Service Type:</label></td>
<td>Select Category:
<cfselect name="catdesc"
bind="cfc:servicetype2.cat_description()"
bindonload="true"/><br />
</td>
</TR>
<tr id="serv_ty2" style="display: inline;">
<td></td>
<td>Select Service:
<cfselect name="service_type"
bind="cfc:servicetype2.getServiceType2({catdesc})"
bindonload="false"/>
</td>
</tr>
This is the form code to use the bind which works but will not display the DATA:
<tr id="lr_verify" style="display: inline;">
<td></td>
<td>Additional Info Reuired:
<cfinput
name="lr_needed"
bind="cfc:servicetype2.getLR({service_type})"
bindonload="no"
disabled="disabled"
display="LAB_REL_NTFN_FLAG"/></td>
</tr>
Any advice on how to get it to display or if I should use a different CF statement with the bind to get the correct display. Thanks in advnace for your help on this.

Salesforce - Process variable number of form fields

I've been able to do this in PHP but it's not translating to Salesforce very well.
I have a form to input an Opportunity. It has an Account, a Contact, and a variable number of fields that will be used to create custom objects (For my purposes an Opportunity is a trip, and the custom objects are legs of that trip). The Salesforce controller needs to create a new Opportunity with the Account and Contact (that's the easy part) but then it needs to create a new custom object (Leg__c) for each leg of the trip.
My form looks like this:
<input type="text" name="Account" />
<input type="text" name="Contact" />
<div id="leg0">
<input type="text" name="dep[0]" />
<input type="text" name="arr[0]" />
</div>
<div id="leg1">
<input type="text" name="dep[1]" />
<input type="text" name="arr[1]" />
</div>
...
I'm not even sure where to begin on this one...
Assuming you know how many legs you need, you can simply create a list of them in your visualforce controller:
public list<Leg__c> liLegs {get; set;};
// upon oppty creation:
liLegs = new list<Leg__c>();
for (integer i = 0; i < iNumLegs; i++)
{
liLegs.add(new Leg__c());
}
Then you can just loop over these in your page like so:
<apex:repeat var="v" value="{!liLegs}">
<apex:inputField value="{!v.Dep__c}"/>
<apex:inputField value="{!v.Arr__c}"/>
</apex:repeat>
The input fields will correspond to the fields in each entry in the list, so then in your Save action or whatever you're using you can just insert the list insert liLegs;.
Hope this is of help and I haven't missed the mark, let me know if so! PS. I've just written this code directly in here so it may not be 100% syntactically correct ;)

Is there an easier way to manipulate checkboxes in coldfusion?

Basically, I have stored information in a database field and I want to display it again through a form.
The original form has the same checkboxes but they are always empty. The user can check the boxes off and the values are put into the database.
Graduate Courses: Fall? Spring? Summer? Stored as a list in a column named grad. (F, M, S)
They have the option to save this lovely form as a draft. When they come back to the draft I need to display what they might have already checked.
<cfif listContains(#grad#, "F") is not 0>
<input type="checkbox" name="grad" id="grad" value = "F" checked />
<cfelse>
<input type="checkbox" name="grad" id="grad" value = "F" />
</cfif>
<cfif listContains(#grad#, "S") is not 0>
<input name="grad" type="checkbox" id="grad" value = "S" checked />
<cfelse>
<input name="grad" type="checkbox" id="grad" value = "S" />
</cfif>
<cfif listContains(#grad#, "M") is not 0>
<input name="grad" type="checkbox" id="grad" value = "M" checked />
<cfelse>
<input name="grad" type="checkbox" id="grad" value = "M" />
</cfif>
Is there an easier method to doing this with cfform code?
Just curious. :)
I do it like this, as I don't really use <cfform>:
<cfloop list="F,S,M" index="g">
<input type="checkbox" name="grad" id="grad_#g#" value="#g#" #CheckedIf(ListFind(grad, g) gt 0)# />
</cfloop>
<!--- ... --->
<cfscript>
function CheckedIf(expression) {
if (arguments.expression) return 'checked="checked"';
return "";
}
</cfscript>
But the ColdFusion docs state:
[since] ColdFusion MX: Changed the cfform tag
preserveData attribute behavior: if it
is set to True, ColdFusion checks
radio and check box values only if
their value matches the posted value
for the control.
So yes, <cfform> can help you with this. Use <cfparam name="FORM.xyz" default="foobar"> to emulate the "posted value" for normal requests.