views:

351

answers:

2

On one page i have list of questions with diffrent tipe of answers. (texboxes, checkboxes etc) Page is dynamicly generated. I have to store answers in database. What is the best way to do this? multiple forms or something else? it would be great if i could send ajax request after every answer but i don't know how to accomplis that (how to detect that user have entered the answer and went to next question)

i'm using asp.net mvc with c#

+1  A: 

You can send only a single form per HTTP request, so to make things more simple, you should only use a single form and give each answer field a unique name that you can then use on the server to map it to the database.

I suggest not to send an AJAX request after the applicant has "finished" with a question. Many people want to answer all questions and then go over the answers a second time.

Therefore, I also advise against using a "Wizard" approach (or a one question per page and you get the next question after submitting the current answer).

Aaron Digulla
well he could serialize all the info in all forms and send one ajax request...although not the best idea!
redsquare
+1  A: 

I agree with Aaron's response that you don't want to save their response using an AJAX request after every question. However, if you ever encounter a situation where you do need to fire an AJAX request based on the user editing data in a form field, you can accomplish this by listening for Javascript events like 'onChange' and 'onBlur' and fire the AJAX request from that event listener.

Hamman359
how could i do it on blur from entire form?
Ante B.
PS: this could solve my problem
Ante B.
You can't do an onBlur for the entire form, it is an element level event. There are a couple of things you could do though. In the onBlur event handler you could loop through all of the form fields to see if they have a value and only fire the AJAX call if they do. A better option may be to do something like setup an array indicating the status of each element (modified or not). Then in the event handler for each element mark it as modified. You could then just check the array, if all elements have been modified, the form is ready to be submitted and you can make the AJAX request.
Hamman359
tnx, this was very helpfull
Ante B.