2

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>
Denoteone
  • 4,043
  • 21
  • 96
  • 150

2 Answers2

4

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> > 
shemy
  • 573
  • 1
  • 5
  • 15
  • I was hoping I wasn't going to have to do it that way because of the shear number of select inputs and options but unless there is another option it looks like that is what I will have to do. Thanks for your feedback. +1 – Denoteone Apr 21 '15 at 06:56
  • if you are taking the option values from any query or list, you can loop them over there. – shemy Apr 21 '15 at 07:04
  • The options are static in the form. Only the default option changes based on the users selection in a previous form. I do not store the other options anywhere other than in the form. If I understand your recommendation correctly. – Denoteone Apr 21 '15 at 07:08
1

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");
});
Dileep N R
  • 11
  • 1