Hi all,
I am working on an interface that communicates with a PostgresSQL to create and grade a multiple choice test. The steps go like this:
1. Get user information
2. Set up a record of the user's test in the database
3. Pull in questions and possible responses from tables.
4. Create a form populated with the results of the query from step #3.
5. Pass form inputs to a handler script that inserts a record for each response.
6. Run a query to compare the user's responses with the possible responses from step #3.
7. Find number of correct responses, calculate score.
8. Display user's responses and whether they were correct or not.

The problem I am running into is in step #5. As there are going to end up being over 100 questions, I want to set the form up dynamically. The approach I am using to create the form is to loop the output of the query that brings in the questions and responses. The input for each response is a radio button defined as such:

<input type="radio" name="q#question_number#" value="#response_letter#">

This will yield radio button names ranging from 1 to however many questions there are. When these are passed to the handler script, I want to run the results through a loop that inserts a record for each response until the number of inputs is exhausted. The problem I am having is in how to define this for the loop. Right now, this is what I've come up with:

<cfloop index="question_number" from="1" to="#q_count#">
    <cfset resp = form.q#question_number#>
    <cfquery name="add_resp" datasource="...">
        INSERT INTO user_responses
            (perstest_id, question_number, user_response, date_entered)
        VALUES
            ('#perstest#', '#i#', '#resp#', '#today_date#')
    </cfquery>    
</cfloop>

The variables:
#q_count# was defined from the record count of a query to count how many questions there are for that test.
#perstest# was defined from a query that pulled in the identifier for this test from the table recording the tests taken by the user.

After trying a number of different approaches, from populating an array to various syntax methods, I come up with the same problem: how do I define the form variables dynamically? If there isn't a short answer to this, I'd appreciate recommendations on tutorials or other documentation that would answer this for me.

Recommended Answers

All 4 Replies

Can you whip up an html page of what you want the results to look like? this might help us answer you question

After doing some more digging around, I think I may have found a solution.

My problem is that I need to construct the variable name in such a way that it can be parsed in the handler script. An idea I found leads me to try this approach:

1. Set up an initial array to define the variable:

<cfscript>
q_items=#count_questions.RecordCount#;
r = ArrayNew(1);
for ( i=1; i LE q_items; i=i+1)
{ 
    r[i]=StructNew();
    r[i].ID=i;
    r[i].Num="q" & i;
    r[i].Ltr=#get_questions.resp_ltr#;
}
</cfscript>

2. Set up a cfloop expression to populate the array based on the results from a query of the question and response db tables.

3. In my handler script, run the cfloop I mentioned before to update the user response table with as many form variables as generated.

So while my approach seemed to be generally correct, the syntax was off. I'll have to play around with the this a bit, but I'll update you on whether this worked or not.

The problem came down to a simple syntax issue in the handler script. In the code snippet I submitted earlier...

<cfloop index="question_number" from="1" to="#q_count#">
    <cfset resp = form.q#question_number#>
    <cfquery name="add_resp" datasource="...">
        INSERT INTO user_responses
            (perstest_id, question_number, user_response, date_entered)
        VALUES
            ('#perstest#', '#i#', '#resp#', '#today_date#')
    </cfquery>    
</cfloop>

...the form variable should be written #form["q" & question_number]#

Now it works as intended. Thanks for being my sounding wall!

Hi all,
I am working on an interface that communicates with a PostgresSQL to create and grade a multiple choice test. The steps go like this:
1. Get user information
2. Set up a record of the user's test in the database
3. Pull in questions and possible responses from tables.
4. Create a form populated with the results of the query from step #3.
5. Pass form inputs to a handler script that inserts a record for each response.
6. Run a query to compare the user's responses with the possible responses from step #3.
7. Find number of correct responses, calculate score.
8. Display user's responses and whether they were correct or not.

The problem I am running into is in step #5. As there are going to end up being over 100 questions, I want to set the form up dynamically. The approach I am using to create the form is to loop the output of the query that brings in the questions and responses. The input for each response is a radio button defined as such:

<input type="radio" name="q#question_number#" value="#response_letter#">

This will yield radio button names ranging from 1 to however many questions there are. When these are passed to the handler script, I want to run the results through a loop that inserts a record for each response until the number of inputs is exhausted. The problem I am having is in how to define this for the loop. Right now, this is what I've come up with:

<cfloop index="question_number" from="1" to="#q_count#">
    <cfset resp = form.q#question_number#>
    <cfquery name="add_resp" datasource="...">
        INSERT INTO user_responses
            (perstest_id, question_number, user_response, date_entered)
        VALUES
            ('#perstest#', '#i#', '#resp#', '#today_date#')
    </cfquery>    
</cfloop>

The variables:
#q_count# was defined from the record count of a query to count how many questions there are for that test.
#perstest# was defined from a query that pulled in the identifier for this test from the table recording the tests taken by the user.

After trying a number of different approaches, from populating an array to various syntax methods, I come up with the same problem: how do I define the form variables dynamically? If there isn't a short answer to this, I'd appreciate recommendations on tutorials or other documentation that would answer this for me.

This is just a sample code that I made. I did this using <input type="text" instead of <input type="radio" but you can give it a try...

Heres my form.

<cfoutput>
	<form method="post" action="dynamic_form_handler.cfm">
		<cfloop from="1" to="10" index="questions">
			<input type="text" name="q_#questions#" /><br/>
		</cfloop>
		<input type="submit" value="SEND" name="submit"/>
	</form>
</cfoutput>

Heres my handler.

<cfif isDefined('form.submit')>
	<cfloop from="1" to="10" index="questions">
		<cfset form_values = form['q_#questions#']> 
		<cfoutput>#form_values#</cfoutput><br/>
	</cfloop>
</cfif>

It is basically the concept when creating a dynamic form variable name.. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.