javascript - Dynamically Adding An Unknown Number of Multiple Select Elements in a Form -
I am working on a form with which the user can add any selection element with multiple attribute sets and I There is a problem getting the results below. My low test case is below, but we have the Example: Result: We want results: However, at least partly It may be wrong, I do not believe that anything works beyond the first level array for the name. Then the field location [] [] can only be the location of the field []. One way to get around this is to submit the form, as presented, using Javascript to send all the selections and encoding them to the server in JSON. Then something like this: area's location [& lt; Fields in the database of the id] and using the name add an existing selection (the user is editing the item) and when the value is submitted, the same array (i.e. array (1,2)) is placed in But when we add new elements using
fieldLocation [] [] and it gives each selection inside its array (array (array (array) (1), array (2))) . What is a way to add element to dynamic without generating a name?
& lt; Form method = "post" & gt; & Lt; Select size = "4" multi = "multiple" name = "field location [] []" & gt; & Lt; Option value = "1" & gt; Item 1 & lt; / Options & gt; & Lt; Option value = "2" & gt; Item 2 & lt; / Options & gt; & Lt; Option value = "3" & gt; Item 3 & lt; / Options & gt; & Lt; Option value = "4" & gt; Item 4 & lt; / Options & gt; & Lt; / Select & gt; & Lt; Input type = "submit" & gt; & Lt; / Form & gt;
array (2) {[0] = & gt; Array (1) {[0] = & gt; String (1) "1"} [1] = & gt; Array (1) {[0] = & gt; String (1) "2"}}
array (1) {[1] = & gt; Array (2) {[0] = & gt; String (1) "1" [1] = & gt; String (1) "2"}}
$ (form_submit_button) .on ('click', function (event) {event.preventDefault (); var data = form serializeArray (); data = JSON.stringify (data); $ (& lt; input / & gt ;, {value: data, type: 'hidden', name: 'field location'}). appendTo (form); form .submit ();}); The example is crude, but you force a click event on the submit button (the actual submit button rather than a normal button or link will be better). Then you can serialize the form and convert it to string. Finally you create a new hidden input element with the data before submitting the form. To remove the data you need, it should allow you to use
json_decode in php. I'm not sure if
serializeArray will work with the name structure, so if it does not, then you will need to manually sort the data.
Comments
Post a Comment