最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Passing a javascript array to a form$_POST without ajax - Stack Overflow

programmeradmin7浏览0评论

I'm working on a form upload element that can be used in the Zend Framework forms. I'm trying to make it so the programmer can use it in any project without having to manually configuring any settings.

The files are uploaded by an AJAX uploader which returns JSON data like:

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

Since the uploader itself is a form element I'm trying to put that data in the form so on the submit the values can be retrieved from the $_POST. I was adding hidden input fields with javascript named uploader-data[] (when submitting the form) but that only allows me to pass 1 variable at the time to the hidden field.

So I guess my question is: "How can I pass the whole array/object to the $_POST / form?". Even though I am using AJAX for the uploader itself I don't want to use it to submit the form. I want a regular form submit containing all the data from the JSON object/array. The files itself are already uploaded but I might want to use the JSON data in my database or at some other place.

Is it possible to do something like this?

Thanks in advance.

I'm working on a form upload element that can be used in the Zend Framework forms. I'm trying to make it so the programmer can use it in any project without having to manually configuring any settings.

The files are uploaded by an AJAX uploader which returns JSON data like:

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

Since the uploader itself is a form element I'm trying to put that data in the form so on the submit the values can be retrieved from the $_POST. I was adding hidden input fields with javascript named uploader-data[] (when submitting the form) but that only allows me to pass 1 variable at the time to the hidden field.

So I guess my question is: "How can I pass the whole array/object to the $_POST / form?". Even though I am using AJAX for the uploader itself I don't want to use it to submit the form. I want a regular form submit containing all the data from the JSON object/array. The files itself are already uploaded but I might want to use the JSON data in my database or at some other place.

Is it possible to do something like this?

Thanks in advance.

Share Improve this question asked Dec 8, 2011 at 14:50 IliansIlians 7437 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Put your javascript value into an input field using JSON.stringify :

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

This will turn your array in the following text value:

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

Zend can parse the JSON value into a php array.

发布评论

评论列表(0)

  1. 暂无评论