Wednesday, 2 October 2013

Submitting multiple values for one input field

Submitting multiple values for one input field

What I'm trying to do:
I'm trying to build a very simple visual layout builder. The idea is that,
the user selects a block, and it has some settings in it, those setting
values are stored in the hidden input, and when the user saves the page, i
store those values in the database.
Basic block is ok:
For example, user selects a 'text' block, it is added like this:
<div>
<input type="hidden" value="text" name="item_name[]">
<input type="hidden" value="" name="item_title[]">
<input type="hidden" value="sdsd" name="item_text[]">
</div>
Problem:
However, some of the blocks have more than one values for each field. For
example, 'gallery' block, which has multiple image urls, image titles etc.
I'm facing problem in finding a suitable way to put together the multiple
values and submit.
Right now I'm adding them to a string with jQuery, separated with __. I
can store the data and separate it, but the problem is that if I want to
remove any image from it, it is very difficult because I have just added
them in the string, so its hard to find it and remove it.
<div>
text item
<input type="hidden" value="gallery" name="item_name[]">
<input type="hidden" value="__http://img1.jpg__http://img2.jpg"
name="img_path[]">
<input type="hidden" value="__img1__img2" name="img_title[]">
<input type="hidden" value="" name="img_desc[]"></input>
</div>
Question:
What would be the suitable way to send multiple values for the above block
example, keeping in the mind that there will be multiple blocks having
multiple input values?
Thanks.

No comments:

Post a Comment