ColdFusion Options removed after selecting option - coldfusion

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>

Related

trying to get categories name along with the images in dynamic table rows

Habing this code for coldfusion but after trying couple of times, i am always ending up showing categories multiple times and images multiple times too, i tried using the group attribute but that it also not doing its work, not sure what is going wrong
here is my complete code
<cfparam name="url.mode" default="">
<cfquery name="ImagesCategories">
select * from rivereast_gallerycategories
</cfquery>
<div id="spacingorder" align="right"><cfoutput>#DateFormat(now(),'long')#</cfoutput></div>
<div class="row">
<div class="col-xs-12 col-sm-12">
<h2>View Gallery Contents</h2>
<div align="right"> </div>
<div align="center">
<cfset variables.newrow = False>
<table align="center" width="100%" cellpadding="1" cellspacing="5" class="borderspacing">
<tr>
<cfif ImagesCategories.recordcount>
<cfoutput query="ImagesCategories">
#gallerycategory#
<cfquery name="filesListings">
select c.galleryCatID,c.ImageID,c.ImageName as name,c.updatedon
from rivereast_gallery c
order by updatedon desc
</cfquery>
<cfloop query="filesListings">
<cfif variables.newrow EQ true>
<tr>
</cfif>
<td height="30" valign="middle">
<div id="previewArea">
<cfset filename = Listlast(filesListings.name,'~')>
<img src="#request.weburl#uploads/#filename#" class="img-fluid"><br>
<input type="checkbox" class="ImagesDelete" name="delItems" data-id="#filesListings.imageID#"> Delete #filesListings.currentRow#
</div>
</td>
<cfif filesListings.currentRow MOD 5 EQ 0>
</tr>
<cfset variables.newrow = true>
<cfelse>
<cfset variables.newrow = false>
</cfif>
</cfloop>
</cfoutput>
<cfelse>
<td colspan="3" align="center"><h2>No Files Found</h2></td>
</cfif>
</tr>
</table>
<cfif filesListings.recordcount>
<div><input type="button" name="deleImages" id="delImages" class="button btn btn-info" value="Delete"></div>
</cfif>
</div>
</div>
</div>
what went wrong here
Hard to say without seeing the data, but it might be this. You are running this query inside a query loop.
<cfquery name="filesListings">
select c.galleryCatID,c.ImageID,c.ImageName as name,c.updatedon
from rivereast_gallery c
order by updatedon desc
</cfquery>
Note that it has no where clause. That could be the reason for seeing the same data more than once.
Assuming your database is somewhat normalized, the recommended fix is to just have one query that gets data from both tables at once. The sql would look something like this.
select JustTheFieldsYouNeed
from rivereast_gallerycategories c join rivereast_gallery g
on c.categoryID = g.categoryID
where some condition is met
I'd join the two queries
SELECT c.*, g.galleryCatID, g.ImageID, g.ImageName as name, g.updatedon
FROM rivereast_gallerycategories c, rivereast_gallery g
WHERE c.galleryCatID = g.galleryCatID
ORDER BY c.gallerycategory, g.ImageName
and then use
<cfoutput query="ImagesCategoriesAndFiles" group="category">
Cleaner code, less confusion.
See example here of how to use cfoutput for grouping record output: https://cfdocs.org/cfoutput

Form counting every record when clicking submit?

I have a form that im outputting results of comments, it can more than one.
I can aprrove these comments all at once, but what I'm trying to do is make it
with the option to submit 1 comment at a time or all comments at a time.
The form at first I wrote it to work on 'submit all', so now with adding a submit
per comment also, I get some errors.
I get because every time I submit a comment (1 comment) its still looking for all the other comments
which I did not submit.
What I think is doing since txtTotalRecords= Mush2.Recordcount, it trying to find the
other records which weren't submitted.
I just can't figure out how I can change this to make it work.
Doing a cfdump on the form i get, meaning there are 11 comments to submit.
How would I be able to change RecordCount to take in every record by itself?
<cfparam name="FormSubmit" type="string" default="FormNotSubmitted">
<cfif isDefined("form.submit")><cfset FormSubmit = "FormSubmitted"></cfif>
<cfif isDefined("form.submit1")><cfset FormSubmit = "FormSubmitted1"></cfif>
<!--- Begin Content ================================================== --->
<cfif FormSubmit eq "FormNotSubmitted" || FormSubmit eq "FormNotSubmitted1" >
<form method="post" action="cse_execoffice_pending.cfm" name="review_comments">
<cfoutput>
<input type="hidden" name="txtApprovedBy" value="#GetCurrentUser.emp_id#">
<!-- count the records that come in from the pending -->
</cfoutput>
<cfoutput query="Mush3">
<form method="post" action="cse_execoffice_pending.cfm" name="review_onecomment">
<input type="hidden" name="txtTotalRecords" value="#Mush2.Recordcount#">
<hr>
<div class="comments_approvaldecision">
<p>
<CFDUMP VAR=#response_id#>
<input type="hidden" name="txtResponseID#mush2.CurrentRow#" value="#response_id#">
<input type="radio" name="execoffice_status#mush2.CurrentRow#" id="approve#CurrentRow#" value="1" checked="checked"> <label for="approve#CurrentRow#">Approve</label><br>
<input type="radio" name="execoffice_status#mush2.CurrentRow#" id="deny#CurrentRow#" value="2"> <label for="deny#CurrentRow#">Deny</label>
</p>
<p> </p>
<p>
<input type="radio" name="star#mush2.CurrentRow#" id="givestar#mush2.CurrentRow#" value="0" checked="checked"> <label for="givestar#CurrentRow#"></i> Give Star!</label><br>
<input type="radio" name="star#mush2.CurrentRow#" id="denystar#mush2.CurrentRow#" value="1"> <label for="denystar#CurrentRow#"></i> No Star</label>
</p>
</div>
</div>
<input type="submit" name="Submit1" value="Submit">
</form>
</cfoutput>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
</cfif>
<cfdump var="#form#">
<cfif FormSubmit eq "FormSubmitted" || FormSubmit eq "FormSubmitted1">
<!--- Get Form Values --->
<cfloop from="1" to="#txtTotalRecords#" index="j">
<h2>test</h2>
<cfset response_id[j] = #Trim(form["txtResponseID" & j])#>
<cfset execoffice_status[j] = #Trim(form["execoffice_status" & j])#>
<cfset star[j] = #Trim(form["star" & j])#>
<cfset commentpositive[j] = #Trim(form["txtCommentPositive" & j])#>
<cfset commentnegative[j] = #Trim(form["txtCommentNegative" & j])#>
<cfset commentpositivereReplace[j] = reReplace(commentpositive[j], '\n', '<br>', 'ALL')>
<cfset commentnegativereReplace[j] = reReplace(commentnegative[j], '\n', '<br>', 'ALL')>
</cfloop>
......... more code...
I believe the code you are getting errors on is not in your sample code listed on this page.
I'm assuming you are doing a to/from loop based upon form.txtTotalRecords.
What you'll want to do is loop over your form items looking for a specific partial form name.
something like this:
<cfloop list="form.fieldnames" index="i">
<cfif left(i,13) IS "txtResponseID">
<cfset thisID = replaceNoCase(i,"txtResponseID","")>
<cfquery>
UPDATE myTable
SET approve = <cfqueryparam value="#form["execoffice_status" & thisID]#">
WHERE ID = <cfqueryparam value="#thisID">
</cfquery>
</cfif>
</cfloop>

Including or excluding range attribute in dynamically generate form field

I am using a query to dynamically create form fields, not all fields use the range attribute.
When using the cfif statement to include or exclude the range attribute I get an error:
See code below:
<cfoutput>
<input type="hidden" name="question_ids" id="question_ids" value="#valueList(rsQuestions.question_id)#">
</cfoutput>
<cfoutput query="rsQuestions" group="modid">
<table border="1" cellpadding="4" cellspacing="4" bgcolor="##0E777A" >
<tr>
<td colspan="2"><span class="style1">#rsQuestions.ModName#</span></td>
</tr>
<cfoutput>
<tr>
<td width="700" bgcolor="##FFFFFF">#rsQuestions.question#</td>
<td width="200" bgcolor="##FFFFFF">
<cfif rsQuestions.question_type_id eq 1>
<cfinput type="text" name="answer_#rsQuestions.question_id#"
message="#rsQuestions.Message#"
tooltip="#rsQuestions.Tooltip#"
validate="#rsQuestions.Validate#"
<cfif #rsQuestions.Range# neq "">
range = "#rsQuestions.Range#"
</cfif>
required="#rsQuestions.mandatory#"
size="#rsQuestions.Size#">
<cfelseif rsQuestions.question_type_id eq 2>
<cfquery name="rsOptions" datasource="dsTest">
SELECT option_id, [option], question_id
FROM questionnaire_question_options
WHERE (question_id = #rsQuestions.question_id#)
</cfquery>
<cfselect enabled="yes"
name="answer_#rsQuestions.question_id#"
multiple="no"
query="rsOptions"
value="option"
display="option">
</cfselect>
</cfif>
</td>
</tr>
</cfoutput>
</table>
</cfoutput>
How can I structure the above statement to include or exclude the 'range' attribute?
As user8675309 (Jenny?) mentioned, you cannot nest <cfif> tags inside another CF tag. So you need to separate those statements out. Here is one way you could do that:
....
<cfif rsQuestions.question_type_id eq 1>
<cfif rsQuestions.Range neq "">
<cfinput type="text" name="answer_#rsQuestions.question_id#"
message="#rsQuestions.Message#"
tooltip="#rsQuestions.Tooltip#"
validate="#rsQuestions.Validate#"
range="#rsQuestions.Range#"
required="#rsQuestions.mandatory#"
size="#rsQuestions.Size#">
<cfelse>
<cfinput type="text" name="answer_#rsQuestions.question_id#"
message="#rsQuestions.Message#"
tooltip="#rsQuestions.Tooltip#"
validate="#rsQuestions.Validate#"
required="#rsQuestions.mandatory#"
size="#rsQuestions.Size#">
</cfif>
<cfelseif rsQuestions.question_type_id eq 2>
....
As mentioned, you can't nest a cfif (or any CF tag) inside another CF tag.
One thing you can do if you really need dynamic attributes is to use the "attributeCollection" attribute.
(ColdFusion 8 or higher.)
Something like:
<cfset inputAttr=structNew()>
<cfset inputAttr.type="text">
<cfset inputAttr.name="answer_#rsQuestions.question_id#">
<cfset inputAttr.message="#rsQuestions.Message#">
<cfset inputAttr.tooltip="#rsQuestions.Tooltip#">
<cfset inputAttr.validate="#rsQuestions.Validate#">
<cfif rsQuestions.Range neq "">
<cfset inputAttr.range = "#rsQuestions.Range#">
</cfif>
<cfset inputAttr.required="#rsQuestions.mandatory#">
<cfset inputAttr.size="#rsQuestions.Size#">
<cfinput attributecollection="#inputAttr#">

File Upload is executing multiple times

I am trying to create a form where by the user selects a location where they then upload a document using CFFILE ACTION ="UPLOAD". I am not great at this so any help you could provide would be greatly appreciated.
Below is my code. Essentially, there is a dropdown list of folder locations. The user first selects one of the locations. Then clicks 'browse' to choose a file. Finally they click submit and the file uploads.
The code that is #dir# is the folder location that is linked to each of the location names. What is happening at the moment is that the document gets uploaded successfully... but it is uploading to all the folder locations instead of the one that is selected from the dropdown. Here is my code:
docuploads.cfm
<cfquery name="getLocation">
SELECT *
FROM Locations
ORDER BY FolderName
</cfquery>
<form action="docuploads.cfm" method="POST" enctype="multipart/form-data" name="upload_form" id="upload_form">
<select name="folderID">
<option value="">--- Select Folder ---</option>
<cfoutput query="getLocation">
<option value="#FolderName#"">#FolderName#</option>
</cfoutput>
</select>
<cfoutput query="getLocation">
<CFIF IsDefined("form.upload_now")>
<CFIF structKeyExists(form, "ul_path") and len(form["ul_path"])>
<CFFILE ACTION="UPLOAD" FILEFIELD="ul_path"
DESTINATION="C:\Documents\#dir#\"
NAMECONFLICT="OverWrite">
<CFSET ClientFilePath = "#clientDirectory#\#clientFile#">
</CFIF>
</CFIF>
</CFOUTPUT>
<br /><br />
Click on the Browse button to select the file to Upload:<br>
<input type="file" name="ul_path" id="ul_path" style="height: 22px;width: 350px;" value=""><br><br>
<input type="submit" name="upload_now" id="upload_now" value="Submit" style="height: 22px;">
<input type="button" name="clear" value="Clear" style="height: 22px;">
<br /><br /><br />
</form>
I hope this makes some sense - can anyone shed any light on getting this to work?
I am assuming that you didnt post the complete code because your #clientDirectory#/#clientFile# don't seem to have a source in your code above.
I put together a possible sample of what It may look like. I also moved your upload routine from the body of your code to the top for readability.
<CFIF IsDefined("form.upload_now")>
<CFIF structKeyExists(form, "ul_path") and len(form["ul_path"])>
<!--- assuming you need to lookup info from the database --->
<cfquery name="getThisLocation">
SELECT FolderID, FolderName, dir
FROM Locations
where foldername=#form.folderid#
</cfquery>
<CFFILE ACTION="UPLOAD" FILEFIELD="ul_path"
DESTINATION="C:\Documents\#getThisLocation.dir#\"
NAMECONFLICT="OverWrite">
<CFSET ClientFilePath = "#getThisLocation.clientDirectory#\##getThisLocation.clientFile#">
</CFIF>
</CFIF>
<cfquery name="getLocation">
SELECT *
FROM Locations
ORDER BY FolderName
</cfquery>
<form action="docuploads.cfm" method="POST" enctype="multipart/form-data" name="upload_form" id="upload_form">
<CFIF IsDefined("form.upload_now")>
<CFIF structKeyExists(form, "ul_path") and len(form["ul_path"])>
<P>FILE UPLOADED</P>
</CFIF>
</CFIF>
<select name="folderID">
<option value="">--- Select Folder ---</option>
<cfoutput query="getLocation">
<option value="#FolderName#"">#FolderName#</option>
</cfoutput>
</select>
<br /><br />
Click on the Browse button to select the file to Upload:<br>
<input type="file" name="ul_path" id="ul_path" style="height: 22px;width: 350px;" value=""><br><br>
<input type="submit" name="upload_now" id="upload_now" value="Submit" style="height: 22px;">
<input type="button" name="clear" value="Clear" style="height: 22px;">
<br /><br /><br />
</form>
I also added a snippet of code to let the user know that the file has been uploaded.
Please try this:
<CFIF IsDefined("form.upload_now")>
<CFIF structKeyExists(form, "ul_path") and len(form["ul_path"])>
<CFFILE ACTION="UPLOAD" FILEFIELD="#form.ul_path#"
DESTINATION="C:\Documents\#dir#\"
NAMECONFLICT="OverWrite">
<CFSET ClientFilePath = "#clientDirectory#\#clientFile#">
</CFIF>
<cfoutput>file uploaded successfully</cfoutput>
</CFIF>
<cfquery name="getLocation">
SELECT *
FROM Locations
ORDER BY FolderName
</cfquery>
<form action="docuploads.cfm" method="POST" enctype="multipart/form-data" name="upload_form" id="upload_form">
<select name="folderID">
<option value="">--- Select Folder ---</option>
<cfoutput query="getLocation">
<option value="#FolderName#"">#FolderName#</option>
</cfoutput>
</select>
<br /><br />
Click on the Browse button to select the file to Upload:<br>
<input type="file" name="ul_path" id="ul_path" style="height: 22px;width: 350px;" value=""><br><br>
<input type="submit" name="upload_now" id="upload_now" value="Submit" style="height: 22px;">
<input type="button" name="clear" value="Clear" style="height: 22px;">
<br /><br /><br />

Where to put a cfquery in a Coldfusion Form

I have a cfquery that is using some of FORM fields in the WHERE clause. My first problem is that every time I access my webpage the cfquery code appears on the top of the page. Where should I put the query within the .cfm form and access some of the fields within the form? My second problem is I'm not sure that the WHERE clause is recognizing the values for the fields. Can you help me please?
Here is the way my code is set up:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="javascript" type="text/javascript">
function addRow() {
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
var iteration = lastRow - 3;
var row = tbl.insertRow(lastRow);
........... etcetra.......
}
</script>
</head>
<body lang=EN-US style='tab-interval:.5in'>
<div class=Section1>
<cfparam name="awardTotals" default="0" />
<cfparam name="search_award.GrandTotal" default="0" />
<cfif isDefined("form.Finalize")>
<cfquery name="search_award" datasource="Test">
SELECT g.Code1 + g.Code2 + g.Code3 + g.Code4 AS GrandTotal
FROM Codes g
WHERE g.CodeNumber = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.CodeNum#">
AND g.TestYear = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.TestYear#">
AND g.SelType = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.SelType#">
AND g.Jurisdiction = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.SelJuris#">
</cfquery>
<cfif not search_award.GrandTotal is FundingTotals>
<script type="text/javascript">
alert('The totals do not match.');
return false;
</script>
<cfelse>
<script type="text/javascript">
alert('The totals match.');
return true;
</script>
</cfif>
</cfif>
<p class=MsoNormal align=center style='text-align:center'><
<cfoutput>
<form name="thisform" action="FormData.cfm" method="post">
<p class=MsoNormal align=left style='text-align:left'>
<input type="hidden" id="totalFields" name="totalFields" value="0">
<input type="text" id="awardTotals" name="#search_award.GrantTotal#" value="0">
<table width="1177" border="1" id="tblSample">
<tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'>
<th height="10"bgcolor="##cccccc" colspan="10"><h3>Jurisdiction:
<select name="SelJuris" id="SelJuris">
<option value = "0">---Jurisdictions---</option>
<option value = "1">Allegany County</option>
<option value = "2">Anne Arundel County</option>
<option value = "3">Baltimore County</option>
<option value = "4">Calvert County</option>
<option value = "5">Caroline County</option>
</select>
<input name="CodeNum" id="CodeNum" type="text" size="20">
<input name="TestYear" id="TestYear" type="text" size="20">
<input name="SelType" id="SelType" type="text" size="20">
</th>
</tr>
</table>
<input type="Submit" name="Submit Form" value="Submit Form" onClick="">
<input type="Button" name="Finalize" value="Finalize" onClick="">
</form>
</body>
</cfoutput>
</html>
(Update from comments)
Right now I'm getting a message that Element CODENUM is undefined in FORM
You want this sort of logic. If the form has been submitted, do something with it. In your case, it would be like this:
<cfif structkeyexists(form, "codenum")>
query, process, display, etc
<cfif>
rest of page
Also, you want to use query parameters for a variety of reasons. So this:
g.CodeNumber = '#form.CodeNum#'
becomes this:
g.CodeNumber = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.CodeNum#">
unless it's a number in which case you change the datatype.
You need add a condition around your cfquery. Also update the name of your submit button without spaces
<input type="Submit" name="SubmitForm" value="Submit Form" onClick="">
<cfif isDefined("form.SubmitForm")>
<cfquery name="search_award" datasource="TrenaTest">
SELECT g.Code1 + g.Code2 + g.Code3 + g.Code4 AS GrandTotal
FROM Codes g
WHERE g.CodeNumber = '#form.CodeNum#'
AND g.TestYear = '#form.TestYear#'
AND g.SelType = '#form.SelType#'
AND g.JurisdictionID = '#form.SelJuris#'
</cfquery>
</cfif>
Please note that in all the examples, a standard html form tag is being used, but the condition for the query is defined in the Coldfusion form scope. I believe you would need to convert the <form> to <cfform> and <input> to <cfinput> like:
...........
<cfinput type="Submit" name="SubmitForm" value="Submit Form" onClick="">
</cfform>
<cfif isdefined('form.submit')>
.............