suleman khan
suleman khan

Reputation: 107

How to get the selected values

I am unable to figure out how to loop over the questionTypes and get selected questionType value. Based on the selected questionType I have to add answers for multioption questions, like in choose the correct answers we provide four choices out of which one we have to select as correct answer.

I have tried to use cfswitch but it does not seem to work:

<html>
  <head> <script src="http://code.jquery.com/jquery-latest.js"></script></head>
<body>
<cfoutput>


    <cfif not IsDefined('rc.questionType')>
        <form class="form form-horizontal" action="#buildUrl('question.new')#" method="post">
            <input type="hidden" name="surveyId" value="#rc.surveyId#">
                <fieldset>

                <div class="control-group">
                <label class="control-label" for="questiontype">Question type</label>
                    <div class="controls">
                    <select name="questionType" onchange="this.form.submit()">
                        <option value="0" >Select question type</option>
                    <cfloop query="rc.types">
                        <option value="#id#">#name#</option>
                    </cfloop> 
                    </select>
                    </div>
                </div> 
                </fieldset>
        </form> 

    <!--- if question type is defined, display question form --->
    <cfelse>
        <form class="form form-horizontal" action="#buildUrl('question.save')#" method="post">
            <input type="hidden" name="id" value="0">
            <input type="hidden" name="surveyId" value="#rc.data.fksurveyId#">
            <input type="hidden" name="questionTypeId" value="#rc.data.fkquestionTypeId#">
            <input type="hidden" name="rank" value="#rc.data.rank#">



            <fieldset>

            <div class="control-group">
            <label class="control-label" for="question">Question</label>
                <div class="controls">
                    <input class="input-xxlarge" type="text" name="question" id="question" required="true" placeholder="write your question">
                </div>
            </div>

            <div class="control-group">
            <label class="control-label" for="Required">Required</label>
                <div class="controls">
                <select name="Required">
                    <option value="1" selected>Yes</option>
                    <option value="0">No</option>
                </select>
                </div>
            </div>                 



            <!--- question arguments for selected type, this will be for multioption questions --->

           <!--- <cfif rc.questiontype is "multiple choice (single selection),Multiple Choice (Multi Selection) with Other,Multiple Choice (Single Selection) with Other,Multiple Choice (Multi Selection)"> --->
           <cfswitch expression="#rc.questiontypeid#">
                <cfcase value="multiple choice (single selection),Multiple Choice (Multi Selection) with Other,Multiple Choice (Single Selection) with Other,Multiple Choice (Multi Selection)">
                 <div class="control-group">
                    <label class="control-label" for="answer">Answer</label>
                      <div class="controls">
                         <input class="input-xxlarge" type="text" name="new_answer" id="new_answer">
                      </div>
                </div>

               <div class="control-group">
                   <label class="control-label" for="rank">rank</label>
                      <div class="controls">
                         <input class="input-mini" type="text" name="rank" id="rank">
                     </div>
               </div>


                <div class="control-group">
                    <label class="control-label" for="answer">Answer</label>
                      <div class="controls">
                         <input class="input-xxlarge" type="text" name="new_answer" id="new_answer">
                      </div>
                </div>

               <div class="control-group">
                   <label class="control-label" for="rank">rank</label>
                      <div class="controls">
                         <input class="input-mini" type="text" name="rank" id="rank">
                     </div>
               </div>
          </cfcase>
        </cfswitch>




            <!---  --->
            <div class="form-actions">
                <button type="submit" class="btn btn-primary">Save</button>
                <a href="#buildUrl(action='admin:question.list', querystring='surveyid=#rc.surveyId#')#" class="btn">Cancel</a>
            </div>
            </fieldset>
        </form> 
    </cfif>
<cfdump var="#rc#">

</cfoutput>
</body>
</html>

This is my controller method to add a new question:

<cffunction name="new" returntype="void" access="public">
        <cfargument name="rc" type="struct" required="true">
        <!---call service to get the textfields, checkboxes etc,. based on questiontype selection --->
        <!--- call service to get question types for select box --->
        <cfset rc.types = getQuestionService().types()> 
        <cfset rc.action = 'New Question'>
        <!--- if user select question type --->
        <cfif isdefined('arguments.rc.questionType')>
            <cfset rc.data = getQuestionService().new(arguments.rc.surveyId, arguments.rc.questionType)>
        </cfif> 
    </cffunction>

I have two forms in single page. In the first form I am selecting questionType. Based on the selected questionType I have to display an add question form.

Upvotes: 0

Views: 1306

Answers (2)

Shawn
Shawn

Reputation: 4786

To make sure I'm reading how this code is supposed to work:

1. This is a form to create the question, not answer it.
2. rc is a struct with your basic question definition as the keys and you have other existing code that insures that the rc structure will exist on this page.
(Rather than isDefined("rc.questionType") I'd use structKeyExists(rc, "questionType"), but that's a whole different discussion.
3. When you change the value of the Question Type, that field is submitted back and other code creates and populates the questionType key of rc struct. So isDefined('rc.questionType') should now be TRUE (moving you to the cfelse block).
4. You are now brought back to the same page with a field to enter the question, whether it's required, and you're looking for the Answer entry boxes based on the questionType.

If all of those assumptions are correct, then this is the point that you would need to loop through your options for your answers. The cfswitch/cfcase is correct, but some of those multiple choice options will need to be handled slightly differently. The ones with "Other" options will need slightly more processing on this end and on the answer tracking end. You'll have to add a text box for that checked answer.

So you'll need a little more definition for the questions in the rc struct. You need to track which one is the correct answer (a simple checkbox). If you want to allow a dynamic number of multi-choice answers, you'll need to keep track of how many answers you'll need. And you could even keep these multi-choice answer options together if you tracked whether the answer was an "Other" or not. This would also allow you to have multiple "Other"-type selections in the multi-choice options. Granted, that would reduce your choices for whether this is a multi-choice question down to just one, simply "Multiple Choice". Let the answers determine whether it's single selection or "Other" selection. And if you wanted to use radio buttons instead of check boxes for the single selects, all you'd have to do is count the answers for the question. Then you can worry about each basic questionType seperately.

The code to better track the answers (correct and "Other" options) will also need to be pulled back up, likely in the getQuestionService() function, which is where I'm assuming you're pulling your question definition and populating the rc struct.

Do you plan on using this form as an UPSERT, or is this simply an INSERT and you'll UPDATE/EDIT the questions elsewhere?

Regardless, back to your original question. The first thing I'd recommend would be to also cfdump the rc struct at the top of the page. See what data you're initially working with.

Down where you need to add your Answers, the cfswitch is the correct method. You likely aren't matching any of your cases here. Output rc.questionTypeID here to see what your value is. And I'd use an integer ID for the question type rather than the name of the question type. It'll give faster, more precise processing. When you get the correct case, you'll need to cfloop your answers here.

To select which one is the correct one, simply add a selected="selected" or checked="checked" (depending on input type) inside of a cfif that checks if the current answer is the correct answer.

Upvotes: 1

Honey
Honey

Reputation: 133

Since the switch expression is set to rc.questiontypeid, the case value should be the possible rc.questiontypeid, not the question type name.

Upvotes: 0

Related Questions