Passing javascript array to form / $ _ POST without ajax

I am working on a form upload element that can be used in Zend Framework forms. I am trying to make it possible for a programmer to use it in any project without having to manually configure any settings.

Files are loaded by the AJAX loader, which returns JSON data, for example:

[ { "name":"image.png", "size":42410, "type":"image\/png", "url":"http:\/\/example.com\/image.png", "thumbnail_url":"http:\/\/example.com\/thumbnail\/image.png", } ] 

Since the loader itself is an element of the form, I am trying to put this data into the form, so that the values ​​can be obtained from $ _POST on submit. I added hidden input fields with javascript named uploader-data [] (when submitting the form), but this only allows me to pass 1 variable at that time to a hidden field.

So, I think, my question is: "How to pass the entire array / object to the form $ _POST /?". Despite the fact that I use AJAX for the loader itself, I do not want to use it to submit the form. I want a regular form to represent all the data from a JSON object / array. The files themselves are already loaded, but I can use JSON data in my database or elsewhere.

Is it possible to do something like this?

Thanks in advance.

+4
source share
1 answer

Put the javascript value in the input field using JSON.stringify :

 data = [ { "name":"image.png", "size":42410, "type":"image\/png", "url":"http:\/\/example.com\/image.png", "thumbnail_url":"http:\/\/example.com\/thumbnail\/image.png", } ] document.getElementById('my_hidden_input').value = JSON.stringify(data); 

This will turn your array into the following text value:

 [{"name":"image.png","size":42410,"type":"image/png","url":"http://example.com/image.png","thumbnail_url":"http://example.com/thumbnail/image.png"}] 

Zend can parse the JSON value in a php array.

+6
source

Source: https://habr.com/ru/post/1385349/


All Articles