问题
Question: (part 1) I am looking for the most efficient way to set my form input values based on the results of my cfquery
. My form fields all match the column names in the database. I know using cfinsert
I can update the database with form input values. Is there a way to do that in reverse?
(part 1.5) How do I set the values of select and radio buttons based on the value of my cfquery?
Background: I have a form with 60+ inputs with a mixture of text
, select
, radio
and textarea
. The page I'm creating is to allow the user to review the answers they have submitted previously and allow them to make changes and submit the form again and update the database with their new answers (if any).
Below is just a small sample of inputs and I how I will set the value unless there is a different way.
<!--variables pulled from the URL-->
<cfset pageAction="#URL.action#">
<cfset rfqID="#URL.rfqID#">
<cfset rfqStatus="#URL.status#">
<!--Query to get previous form answers -->
<cfquery name="getFormData" datasource="RC">
SELECT *
FROM RFQ_Data
WHERE form_ID = <cfqueryparam value="#ARGUMENTS.rfqID#">
</cfquery>
<cfform name="rfq_form" class="pure-form pure-form-aligned" enctype="multipart/form-data" action="rfq_action.cfm" method="POST">
<cfoutput>
<label>*Sold to Party:</label>
<cfinput type="text" name="sold_to_party" value="#getFormData.sold_to_party#"/>
<!--HOW DO I SET THE DEFAULT VALUE OF MY SELECT TO BE THE VALUE FOUND IN THE CFQUERY?-->
<label>*Product Type:</label>
<select name="product_category" id="product_category">
<option value="ts8-it">TS8-Data Center </option>
<option value="ts8-ie">TS8-Industrial </option>
<option value="WM_AE_JB">WM/AE/JB </option>
<option value="other">Other </option>
</select>
<h3>Additional information:</h3>
<textarea name="additional_info_datacenter" rows="10" cols="60" style="margin-left:40px;">#getFormData.additional_info_datacenter#</textarea>
<!--HOW DO I SET THE DEFAULT VALUE OF MY RADIO TO BE THE VALUE FOUND IN THE CFQUERY?-->
<label>19" Rails</label>
<input id="rails_yes" type="radio" name="19_Rails" value="yes"> YES
<input id="rails_no" type="radio" name="19_Rails" value="no"> NO
</cfoutput>
<cfinput style="padding:4px 6px;" type="submit" value="Submit Current Order" name="submit"/>
</cfform>
回答1:
for select you can try a comparison for each option values like:
<select name="product_category" id="product_category">
<option value="ts8-it" <cfif CompareNoCase(getFormData.product_category,"ts8-it") EQ 0>selected="selected"</cfif> >TS8-Data Center </option>
Same way you can try the radio fields,
<input id="rails_yes" type="radio" name="19_Rails" value="yes" <cfif getFormData.19_Rails>checked="checked"</cfif> >
<input id="rails_no" type="radio" name="19_Rails" value="no" <cfif NOT getFormData.19_Rails>checked="checked"</cfif> >
回答2:
Please try the following jQuery code snippet.
var selDefualtVal = '#getFormData.product_category#';
jQuery(document).ready(function () {
jQuery("#product_category option[value='"+selDefualtVal+"']").attr("selected","selected");
});
来源:https://stackoverflow.com/questions/29764370/coldfusion-how-to-set-form-input-values-from-the-results-of-a-cfquery