I have a form where a user rates a poem from 1 to 3. My code is as follows:
<select name="rating">
<cfif len(duplicateCheck.score)><option value="#duplicateCheck.score#">You scored: #duplicateCheck.score#</option>
<cfelse><option value="">– Rate This Poem –</option>
</cfif>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
If the user has already rated the poem, I am trying to make their previous score be selected. If not, the user can select 1-3. How should I do this?
Depends on how you're storing the fact that the user has already rated the poem. But from a high level:
<option value="1"<cfif userHasSelected eq 1> selected="selected"</cfif>>1</option>
<option value="2"<cfif userHasSelected eq 2> selected="selected"</cfif>>2</option>
<option value="3"<cfif userHasSelected eq 3> selected="selected"</cfif>>3</option>
So, do you already have a handle on whether or not the user has rated the poem? Or is that the actual question?
If you loop through your list of options you could do this dynamically.
<cfloop from="1" to="3" index="thisOption">
<option value="#thisOption#" <cfif userHasSelected eq thisOption> selected="selected"
</cfif>>#thisOption#</option>
</cfloop>
Or you can move the code to select the drop down out of the option html, which i prefer.
<cfloop from="1" to="3" index="thisOption">
<cfset variables.selected = userHasSelected eq thisOption? 'selected' : '' />
<option value="#thisOption#" #selected#>#thisOption#</option>
</cfloop>
Related
The selected text is not staying selected in a drop down menu on my order page.
I've added a drop down menu to our order page to select the words "UPS Account Number, or "FedEx Account Number". Then an input box gets displayed for the user to type in their account number. I am using the onChange event with this drop down menu. This works fine on a page by itself, but when I try to use it on our order page, the selected text in the drop down menu does not stay selected. If I change the cfset to page.ShipAcctMethod, it errors out.
<cfif isDefined('form.ShipAcctMethod')
AND form.ShipAcctMethod NEQ "">
<cfset form.ShipAcctMethod = form.ShipAcctMethod>
</cfif>
<select name="ShipAcctMethod" required="yes" onChange="this.form.submit()">
<option value="" selected>Shipping Account</option>
<option value="UPSNumber" <cfif isDefined('page.ShipAcctMethod')
and form.ShipAcctMethod eq 'UPSNumber'>selected</cfif>>
UPS Account Number
</option>
<option value="FedExNumber" <cfif isDefined('page.ShipAcctMethod')
and form.ShipAcctMethod eq 'FedExNumber'>selected</cfif>>
FedEx Account Number
</option>
</select>
<br><br>
<!--- Choose a selection --->
<cfif isDefined('form.ShipAcctMethod') and
form.ShipAcctMethod eq 'UPSNumber'>
<input type="text" maxlength="100"
size="30"name="UPSNumber"
placeholder="UPS Account Number" required>
</td>
</tr>
<cfelseif isDefined('form.ShipAcctMethod')
and form.ShipAcctMethod eq 'FedExNumber'>
<input type="text" maxlength="100"
size="30"name="FedExNumber"
placeholder="FedEx Account Number" required>
</td>
</tr>
I need the selected text to stay selected because this is a requirement the user has to choose. How do I get the selected text to stay selected?
Sorry, I don't have the ability to comment since I don't have a 50 reputation, so I'm providing this as an answer. As Ageax said, the problem is because isDefined('page.ShipAcctMethod') should be isDefined('form.ShipAcctMethod'). Because of this, the condition will always evaluate to false and the "selected" attribute never gets set for those options.
For the life of me I can't get CFIF to give me a true result when using the EQ operator with 2 equal fields . Below is the CF code:
<select class="form-control" name="sales_contact_id">
<cfoutput query="rc.getPartnerContacts">
<cfif rc.getPartnerContacts.id EQ rc.getPartner.sales_contact_id>
<cfoutput><option value="#rc.getPartnerContacts.id#" selected="selected">#fname# #lname#</option>
</cfoutput>
<cfelse>
<cfoutput><option value="#rc.getPartnerContacts.id#">#fname# #lname# #rc.getPartner.sales_contact_id#</option>
</cfoutput>
</cfif>
</cfoutput>
</select>
I put the #rc.getPartner.sales_contact_id# into the option text to show the value of the variable.
And this is the HTML output:
<select name="sales_contact_id" class="form-control">
<option value="1">David Elliott 2</option>
<option value="2">James Knight 2</option>
<option value="3">Fred Bloggs 2</option>
<option value="4">John Doe 2</option>
<option value="5">Jane Smith 2</option>
</select>
The value of the rc.getPartner.sales_contact_id field is 2 which the same as the id field for the 'James Knight' record in the rc.getPartnerContacts recordset. The CFIF statement should be true for the James Knight record so that this option is the default within the drop down list. I've tried using val(), I've tried subtracting one from the other and then using a CFIF EQ 0 and still won't work!
I'm sure that this is a really simple mistake but I can't see it!!!
This is a formatted comment. When in doubt, look at your data.
<cfdump var="#rc.getPartnerContacts#">
<cfoutput query="rc.getPartnerContacts">
<cfif rc.getPartnerContacts.id EQ rc.getPartner.sales_contact_id>
yes
<cfelse>
no ID is #id# and sales contact id is #rc.getPartner.sales_contact_id#
</cfif>
<br>
A facepalm moment - The <cfoutput> within a <cfoutput> caused the problem.
I'm having trouble with my ColdFusion code. I'm trying to create a simple HTML select form using cfloop to populate the options. it works, and when you select an option, it pulls that option from the db. but once you do that, the only option available is the option previously selected. What am I doing wrong?
<!--- Query the DataBase --->
<cfparam name="url.colors" default="">
<cfif structKeyExists(form, "colordb")>
<cfset url.colordb = form.colordb>
</cfif>
<cfquery datasource="bentest" name="colors">
SELECT *
FROM color_codes
<cfif structKeyExists(url,"colordb") and isNumeric(url.colordb)>
WHERE id=#url.colordb#
</cfif>
</cfquery>
<!--- Add Selector for user to select a color --->
<div class="selector">
<cfoutput>
<form action="?contentId=colorPickdb" name="clr" method="post" class="clr">
<select class="clr" name="colordb" onChange="submit();">
<option selected>Select A Color!</option>
<cfloop query="colors">
<option value="#colors.id#">#colors.color#</option>
</cfloop>
</select>
</form>
</cfoutput>
</div>
<div class="dump">
<!--- Output results of Query --->
<p><cfif structKeyExists(form, "colordb")>
<cfoutput query="colors">
#colors.color# <br>
#colors.hexvalue# <br><br>
</cfoutput>
</cfif>
<br>
</p>
</div>
You can use query of query here.
<!--- Query the DataBase --->
<cfparam name="url.colors" default="">
<cfquery datasource="bentest" name="colors">
SELECT *
FROM color_codes
</cfquery>
<!--- Add Selector for user to select a color --->
<div class="selector">
<cfoutput>
<form action="?contentId=colorPickdb" name="clr" method="post" class="clr">
<select class="clr" name="colordb" onChange="submit();">
<option selected>Select A Color!</option>
<cfloop query="colors">
<option value="#colors.id#">#colors.color#</option>
</cfloop>
</select>
</form>
</cfoutput>
</div>
<div class="dump">
<!--- Output results of Query --->
<p>
<cfif structKeyExists(form, "colordb")>
<cfquery dbtype="query" name="colorSelected">
SELECT *
FROM colors
WHERE id=#form.colordb#
</cfquery>
<cfoutput query="colorSelected">
#colorSelected.color# <br>
#colorSelected.hexvalue# <br><br>
</cfoutput>
</cfif>
<br>
</p>
</div>
I have a small form that loops from 1 to 30 and assigns variables. After these variables are saved to the database, I am having some trouble figuring out how to set the value of the input as the value from the database. Here is a some of my code.
<cfloop index="i"
from="1"
to="30">
<select name="supply_#i#"
id="supply_#i#"
/>
<cfloop query="GetDescriptor">
<option value="#GetDescriptor.Code#">
#GetDescriptor.Descriptor#
</option>
</cfloop>
</select>
<input type="text"
name="quant_#i#"
id="quant_#i#"
/>
</cfloop>
Once complete, the user saves all 30 values to the database. When they attempt to view or edit the form, I need to show the previously selected value. I really don't know how to "nest" coldfusion variables together, or if it is even possible, but here is an example of the VIEW/EDIT .cfm that does not work, but shows you what I am trying to accomplish with the values.
<cfloop index="i"
from="1"
to="30">
<cfset "supply_#i#" = #QUERY.supply#i##>
<cfset "quant_#i#" = #QUERY.quant#i##>
<select name="supply_#i#"
id="supply_#i#"
/>
<cfloop query="GetDescriptor">
<option value="#GetDescriptor.Code#"
<cfif #VARIABLES.supply_#i## EQ '#GetDescriptor.Code#'>
SELECTED="SELECTED"
</cfif> >
#GetDescriptor.Descriptor#
</option>
</cfloop>
</select>
<input type="text"
name="quant_#i#"
id="quant_#i#"
value ="#VARIABLES.quant_#i##"
/>
</cfloop>
I think the easiest way to accomplish this is to use array notation. That is, use brackets instead of dots. Since all variables scopes are structs you can do this.
variables["foo"]
is equivalent to
variables.foo
So in your case, you probably want to reference
#VARIABLES["supply_#i#"]#
or, perhaps
#VARIABLES["supply_" & i]#
(Too long for comments)
What is the actual structure of the db table the values are inserted into? The variable names suggest you are using a denormalized db design. If so, consider normalizing the structure and storing the data in rows (SupplyCode, Quantity), rather than columns (Supply1,Qty1,Supply2,Qty2,....). The normalized structure is a lot easier to query, and supports as many or as few entries as needed.
Simply query the table and use a query loop to display the existing values. Use currentRow in place of the index variable i:
<cfquery name="existingEntries" ...>
SELECT SupplyCode, Quantity
FROM YourTable
ORDER BY .....
</cfquery>
<cfloop query="existingEntries">
<select name="SupplyCode_#currentRow#" id="SupplyCode_#currentRow#">
<cfloop query="GetDescriptor">
<option value="#GetDescriptor.Code#"
<cfif existingEntries.SupplyCode EQ GetDescriptor.Code>
SELECTED="SELECTED"
</cfif>>
#GetDescriptor.Descriptor#
</option>
</cfloop>
</select>
<input type="text" name="Quantity_#currentRow#" value ="#existingEntries.Quantity#" .... />
</cfloop>
NB: Typically you would store a FK ID from the GetDescriptor table, ie "SupplyID" rather than a string code or description, ie "SupplyCode"
I'm working on code where I am getting two lists. I'm trying to use those lists to pre-select items in select box having the "multiple" attribute. However, I am unable to maintain the selections.
Code:
<select name="sbbox" id="sbbox" class="can" multiple>
<cfloop list="#lstFinds#" index="k" delimiters=",">
<option value="#k#" <cfif #listfindnocase(k,getproducts.ptype)#>selected</cfif>>#k#</option>
</cfloop>
</select>
Sample values:
lstFinds = abc,xyz
getproducts.ptype also contains values like abc,xyz
I want to keep both selected, if both values exists for the user. If one exists, keep one selected. If none are selected, keep none.
I also tried using listContains, but it did not work.
Transferred from pastebin linkL
The ptype values are coming as comma separated in the database ie "abc,wed,mon,def". Whatever those values are, I need to match and selected the ones which have the same value in the listFind. I hope I made it clearer.
<cfset lstFinds = 'abc,xyz,def,www,kkr,mon,tue,wed'>
<cfquery name="getproducts" datasource="cdfg">
select ptype
from
mytable
where
ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#id#">
</cfquery>
<select name="sbbox" id="sbbox" class="can" multiple>
<cfloop list="#lstFinds#" index="k" delimiters=",">
<option value="#k#" <cfif #listfindnocase(k,getproducts.ptype)#>selected</cfif>>#k#</option>
</cfloop>
</select>
There are three things wrong with this:
<cfloop list="#lstFinds#" index="k" delimiters=",">
<option value="#k#" <cfif #listfindnocase(k,getproducts.ptype)#>selected</cfif>>#k#</option>
</cfloop>
First, as others have mentioned, in the listfindnocase function, the list comes first.
Next, the reason you are not getting the desired results after sorting out the first problem, is that getproducts.ptype is not a list. It is the value from the first row of the query. To get all the values in a list, use the valuelist() function.
Finally, the correct syntax for having an option selected is selected="selected". So the code block above should be this:
<cfloop list="#lstFinds#" index="k" delimiters=",">
<option value="#k#"
<cfif listfindnocase(ValueList(getproducts.ptype), k)>
selected="selected"
</cfif>>#k#
</option>
</cfloop>
Ok, the asker provided the following code via a Pastebin
<cfset lstFinds = 'abc,xyz,def,www,kkr,mon,tue,wed'>
<cfquery name="getproducts" datasource="cdfg">
select ptype
from
mytable
where
ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#id#">
</cfquery>
these are coming from database column ptype of a table as: abc,wed,mon,def
<select name="sbbox" id="sbbox" class="can" multiple>
<cfloop list="#lstFinds#" index="k" delimiters=",">
<option value="#k#" <cfif #listfindnocase(k,getproducts.ptype)#>selected</cfif>>#k#</option>
</cfloop>
</select>
I ran the following code as at cflive.net
<cfset lstFinds = 'abc,xyz,def,www,kkr,mon,tue,wed'>
<cfset getProducts = {pType = "abc,wed,mon,def"}>
<cfoutput><select name="sbbox" id="sbbox" class="can" multiple>
<cfloop list="#lstFinds#" index="k" delimiters=",">
<option value="#k#" <cfif #listfindnocase(getproducts.ptype,k)#>selected</cfif>>#k#</option>
</cfloop>
</select></cfoutput>
When I run this code, abc, def, mon, and wed are selected. Should these not be selected, or should others be checked?
This code appears to be doing what you are asking. The only changes I made
Changed the logic as per what Matt suggested in question comments.
Because I don't have the data to run a query against, I setup a getProducts structure with the key "pType" and the values as Asker suggested they might be. For the purpose here, my sample struct is functionally identical to a one-row query.
Finally, is it possible that you're trying to pull this from multiple rows? You might try
<cfset variables.ptypes = valuelist(getproducts.ptype)>
<cfoutput><select name="sbbox" id="sbbox" class="can" multiple>
<cfloop list="#lstFinds#" index="k" delimiters=",">
<option value="#k#" <cfif #listfindnocase(variables.ptypes,k)#>selected</cfif>>#k#</option>
</cfloop>
</select></cfoutput>