Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Action Page

Status
Not open for further replies.

sicollier

Programmer
Jul 29, 2003
4
GB
Hi Guys,

Thanks for the advice a couple of weeks ago.

The problem i've got is i have to design a questionnaire producing 12 random questions out of 21, that's not a problem ive done that.

It's the Action Page that's causing me problems.

I've designed five tables in the Database

1.users (id, name , mail etc) which i can get from the session.
2.Answers (ans_id,Ques_id, body, result) Which hold data.
3. questions (id, body)
4. Test_info (user_id, question_id, answer_id)
5. Results (user_id, overall_result)


As mentioned before i've run a couple of cfqueries in the question page thats working fine, but i need to design an action page to input the data into the relevant tables.

I've been at it two days now, but with no luck.

Has anybody got any ideas, any help would be great

Thanks,
Si..........
 
All of it really Carl, i can get the info from the session for the user table, it's the rest i'm struggling with.

I can't figure out how to put the data into the relevant tables.


It's pretty hard to answer such a general question without writting your whole app for you... which is kind of out of the scope of what these forums are designed to provide (and what most developers are designed to provide... free of charge).

But, one general solution would be something like:

- Set up a structure that defines your questions, say:
Code:
<CFSCRIPT>
   strQuestions = StructNew();

   sQuestionID = &quot;ques1234&quot;;
   strQuestions[sQuestionID] = StructNew();
   strQuestions[sQuestionID][&quot;question&quot;] = &quot;What is question 1?&quot;;
   strQuestions[sQuestionID][&quot;answers&quot;] = ArrayNew(1);
   strQuestions[sQuestionID][&quot;answers&quot;][1] = &quot;It is this&quot;;
   strQuestions[sQuestionID][&quot;answers&quot;][2] = &quot;It is that&quot;;
   strQuestions[sQuestionID][&quot;answers&quot;][3] = &quot;It is the other&quot;;

   sQuestionID = &quot;ques5678&quot;;
   strQuestions[sQuestionID] = StructNew();
   strQuestions[sQuestionID][&quot;question&quot;] = &quot;What is question 2?&quot;;
   strQuestions[sQuestionID][&quot;answers&quot;] = ArrayNew(1);
   strQuestions[sQuestionID][&quot;answers&quot;][1] = &quot;Blue&quot;;
   strQuestions[sQuestionID][&quot;answers&quot;][2] = &quot;Red&quot;;
   strQuestions[sQuestionID][&quot;answers&quot;][3] = &quot;Green&quot;;

        :

</CFSCRIPT>
until you have 21 questions, each with their associated answers. You can code this into a separate .cfm file that you include in both the form page and the ok/action page... so you only have to maintain it in one place. If you already have this info in a query as you mentioned, you can go that way too. You would simply do a join on the question table and the answer table, like:
Code:
<CFQUERY name=&quot;qryQuestions&quot; ...>
  SELECT *
  FROM questions q, answers a
  WHERE q.id = a.ques_id
  ORDER BY q.id, a.ans_id
</CFQUERY>

- Determine the 12 random questions out of the 21 you want to display for the current user. There are several ways you could do this, but perhaps the easiest to understand and maintain is something like:
Code:
<CFSET Randomize(GetTickCount())>
<CFSET lstAllQuestions = StructKeyList(strQuestions)>
<CFSET lstCurrentQuestions = &quot;&quot;>
<CFLOOP condition=&quot;ListLen(lstCurrentQuestions) LT 12&quot;>
   <CFSET sTempQuestion = ListGetAt(&quot;#lstAllQuestions#&quot;,RandRange(1,ListLen(&quot;#lstAllQuestions#&quot;)))>
   <CFIF ListFind(&quot;#lstCurrentQuestions#&quot;,sTempQuestion) LTE 0>
      <CFSET lstCurrentQuestions = ListAppend(&quot;#lstCurrentQuestions#&quot;,sTempQuestion)>
   </CFIF>
        :
</CFLOOP>

- Output the questions on the form
Code:
<CFLOOP list=&quot;#lstCurrentQuestions#&quot; index=&quot;whichQuestion&quot;>
   <CFOUTPUT>#strQuestions[whichQuestion][&quot;question&quot;]#</CFOUTPUT><br />
    <CFLOOP from=&quot;1&quot; to=&quot;#ArrayLen(strQuestions[whichQuestion][&quot;answers&quot;])#&quot; index=&quot;whichAnswer&quot;>
       <CFOUTPUT><input type=&quot;radio&quot; name=&quot;#whichQuestion#&quot; value=&quot;#whichAnswer#&quot;>#strQuestions[whichQuestion][&quot;answers&quot;][whichAnswer]#<br />
    </CFLOOP>
</CFLOOP>
now you'll have 12 sets of radio buttons, all named after the question id to which they pertain. If you went with a query to hold all your questions/answers, you'd simply output them like:
<CFOUTPUT query=&quot;qryQuestions&quot; group=&quot;id&quot;>
#qryQuestions.questionBody#<br />
<CFOUTPUT group=&quot;ans_id&quot;>
<input type=&quot;radio&quot; name=&quot;#qryQuestions.ques_id#&quot; value=&quot;#ans_id#&quot;>qryQuestions.answerBody#<br />
</CFOUTPUT>

</CFOUTPUT>





- Include fields for your user info, etc, on the form as well. Make sure the form is set up as method=&quot;POST&quot;, so all the variables get passed in the FORM scope.

- In the code for the action page, simply look through the FORM scope structure until you recognize the questions
Code:
<CFLOOP collection=&quot;#FORM#&quot; item=&quot;whichField&quot;>
   <CFSET strQuestion = StructFind(strQuestions,whichField)>
   <CFIF IsStructure(strQuestion)>
       <CFSET sQuestionID = whichField>
       <CFSET sAnswerID = FORM[whichField]>
               :
       <!--- you know that the user answered #sAnswerID# to #sQuestionID# --->
       <!--- process this info, or stuff into a database as appropriate --->
   </CFIF>
</CFLOOP>

Is that enough to go on?



-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top