I am creating a booking form. It can manage up to 6 attendees, and each attendee can be asked any number of questions.
I want to find the most efficient way to write the question responses to the database. The responses have the following fields:
contact_id (integer)
question_id (integer)
response (text paragraph including line breaks)
comment (text paragraph including line breaks)
various number fields (integer)
and I expect to have 10-100 individual responses which need writing to the database as individual rows to a table called responses.
I have been planning to store the responses on the client side in a Data Store ds_responses as they are entered by the user.
I would like to minimise the amount of database resources that are used in getting this done.
These seem to be my options, along with some questions:
Write a flow that repeats through the records in the ds_responses Data Store and calls a server action to save each response individually.
This I can do but seems expensive in terms of server and database usage.
Pass the entire contents of the ds_responses Data Store to a single server action call and then either repeat through the records or send the whole record set to a stored procedure.
This seems more efficient.
QUESTION - How can I pass the entire contents of the ds_responses Data Store in one go? Can I do it in some way via JSON? What Wappler feature would I use to do this?
I may have misunderstood your task, but if I understood correctly, I would send all the responses in one form as an array with an unlimited number of responses.
Unfortunately, I closed the domain where there was a demonstration of how it looks live. But if you describe it in words, this is a form in which you can create any number of inputs. And when you send it to the database, an array is sent, which is then stored in the database.
Just a quick thought, if you’re concerned about storing 10’s or 100’s of rows per question/attendee, you could store their response as JSON in a single row in your db instead
@Antony - I'm working on a complex query at the moment to pass data (29 + filters, each can have multiple data points) via a SC form to an API. So, while not exactly the same - the intent of passing data from a form to SC so you can then action has similarities and you might find the following useful, and a way to avoid using side data store.
SC forms can output as nested JSON depending on how you name the input.
I'd say you could nest the questions and answers using the implementation options used in w3 examples above, create the nested $POST parameters and then use that data how you wish for repeats / inserts.
Well I really don’t know if I need a hidden field per variable or not, but after trying lots of options that is the direction I am going in unless someone shows me I can use just one field for the whole array.
In my case, my form is not within a repeat loop (which creates a $index). I just have one hidden form with hidden inputs which I submit with a formname.submit() command.
I am just wanting to output the entire contents of the responses data store, so responses.data.
So I can try your example, but I’m not sure where the value of {{index}} is going to come from.
But yes, I realise do need to have dmx-bind:name= rather than just name=. Thanks for getting me straight on that!
@s.alpaslan, I’m not sure how your last message helps me… my issue is how to specify the dmx-bind:name="" field in my HTML in order to get the data store to pass to the server action.
@patrick, do you know how I can specify the name field in my form input to pass my array over to the server action?
I think I’m with you now. So if you’re expecting to receive an array (responses) on the server side you’re going to need to use a repeat to cycle through the multiple records from datastore, that’s why $index is used to determine the record (repeat) id.
If it’s just a single record (response) being posted to your server action then all you need is the variables in your POST globals.
Edit: It’s very similar to the shopping cart scenario where you may have multiple cart lines in datastore that you need to insert to the db, and each line contains variables such as product_id, product_price, qty, etc…