1

I am building an application that has a set of questions and answers. A user would respond to each question and finally click the 'save' button.

I wish to store the combination of the question and response as array of structures. Each entry in the array would be a structure of question_id and response_id.

Once the user clicks the 'save' button, I need to save the entries in the database.

  • How would I pass the array of structures to a cfc?
  • How would I process the array of structures to insert them into the database?

Please help.

1
  • What do you have so far? Commented Jan 7, 2011 at 23:08

1 Answer 1

2

As a form, each field is part of the FORM scope upon submit by default. This scope is a simple struct (i.e. FORM = {question_id: value, response_id: value}), from which you can easily reference each item: (for output) <cfoutput>#FORM.question_id#</cfoutput>.

So let's say you had 10 questions, and that there would always be an answer. First, you'd have a hidden form field for each question:

<input type="hidden" name="questionId_01" value="01" />
<input type="hidden" name="questionId_02" value="02" />

Notice each field has a different name. This is necessary to get a unique reference to each field. You then create corresponding input fields for each question. Say it was multiple choice questions, and you were using radio buttons. You'd create your fields with the same name but the choice values:

<input type="radio" name="responseId_01" value="a" />
<input type="radio" name="responseId_01" value="b" />
<input type="radio" name="responseId_01" value="c" />

<input type="radio" name="responseId_02" value="a" />
<input type="radio" name="responseId_02" value="b" />
<input type="radio" name="responseId_02" value="c" />

Submitting your form would pass these as simple name/value pairs to the server. I see the jQuery tag attached to your question (though you don't mention it), so I'll assume you're doing an Ajax submission. Most people use $.serializeArray() to put their form data in a format for ajax data. It's the simple way of handling it.

$('myForm').submit(function(e){
  e.preventDefault();

  $.ajax({
    url: 'myProcessor.cfc?method=processForm&returnformat=JSON',
    data: $(this).serializeArray(),
    success: callbackFunctionName
  });
});

Will post the following params:

questionId_01   01
questionId_02   02
responseId_01   b
responseId_02   a

method   processForm
returnformat   JSON

So, you don't have the array of structures you're looking for server-side yet. You can do some server-side processing to make your arrays by looping over the ARGUMENTS scope:

<cfscript>
  LOCAL.processArr = ArrayNew(1);

  for (LOCAL.i in ARGUMENTS){
    if (FindNoCase('questionId',LOCAL.i)){
      LOCAL.tmpArr = ListToArray(LOCAL.i,'_'); // get the Id to find response entity
      LOCAL.tmpStr = {questionId = ARGUMENTS[LOCAL.i], responseId = ARGUMENTS['responseId_' & LOCAL.tmpArr[2]]}; // CF arrays start at 1
      ArrayAppend(LOCAL.processArr, Duplicate(LOCAL.tmpStr));
    }
  }
</cfscript>

Now you have the array of structs you were looking for. You can then loop the array and perform your inserts.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.