AJax技术

jQuery AJAX Form, cannot send array?

字号+ 作者:H5之家 来源:H5之家 2017-10-25 18:18 我要评论( )

jQuery AJAX Form, cannot send array?-程序代码库-The program code library

收录日期:2017/10/25 18:18:12 时间:2010-06-29 09:04:17 标签:ajax,jquery

I'm practicing my jQuery skills (ok, learning too) and experienced an issue. I got a file upload form with file input. I'm using this plugin (#tab-Uploading) to upload multiple files at once. So I'm using following form input:

<input type="file" accept="gif|jpg|jpeg|png" maxlength="5"/>

Now... I'm trying to send an AJAX request to php file that will handle upload and server-side validation:

$('#upload_photos_s').click(function(q){ var photo = $('[name=photo[]]').val(); // Process form $.ajax({ type: "POST", url: "upload.php", data: 'photo[]='+photo, success: function(html){ alert($('[name=photo[]]').val()); $("#photo_upload_form").html(html); } }); return false; });

While using Firebug I can see that there's only one file in photo[].

Any suggestions why? Is there something I missed?

Regards, Tom

As it stands, you are indeed querying the value of the first photo[] member only. photo[].val() will not return an array containing all the values.

You would have to run through each member of photo[], e.g. using each(), to build an array of values.

However, I'm not sure this is the right path to go for whatever you want to do. You are aware that what you are doing is uploading the file names only, not their data?

It is not possible to upload files using AJAX without the help of additional tools like Flash-based SWFUPload. This is for security purposes to prevent scripts from having direct access to local files.

Maybe what you're trying to do is best suited for an approach where the form's target property points at an <iframe>. That would not trigger a reload of the page, but still submit the form the "traditional" way, allowing for old-school file uploads.

Well on the link you provided, there is a part called Ajax specifying the simplest way is to use the jQuery Form Plugin.

Documentation of plugins help a lot usually ^^.

Have a nice day :)

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评