GG资源网

使用Ajax提交文件上传的WordPress表单

问题描述:

I am trying to create form and submitting this with ajax. So the page wont reload. I don\'t have a lot of experience with ajax and I am trying to find as much information as I can.

Right now in my code I can sbmit form without reloading. But I have a field for uploading files. I know It is a bit different to do this and I also found some examples but so far no luck. For example Ajax file upload in WordPress - can\'t pass FormData

Right now I have My Ajax code like this:

Ajax

My form:

Right now this is how far I got. This is not my full form I have already added nonce and other required settings for sequrity. When I check input file It keep displaying error message that the field is empty.

My form handler

So My question is What can I do or add in my ajax or form handler to get file upload submit with Ajax working. Any suggestion, advice or some existing example that are familiar will be appreciated. I am practicing with WordPress and Ajax that is how far I got.

I have tried to use FormData but still no luck so far.

网友观点:

Here is some tips to make work your provided form:

Form:

I removed placeholder=\\\"<?php echo $field->label ?>\\\" from your text input, because it\'s coming from custom code, which you didn\'t provide.

Ajax.js:

NOTE: You\'re using var form_elm = jQuery(this);, which returns object of jquery with all Form content. That\'s why I used it for accessing input information. Instead you can access to form inputs with their name, class, id, placeholder and etc.

What changed:

  1. removed (function($) {} wrapper and instead used jQuery(document).ready(function ($) {});. This is just for note. There is no need to wrapp your jquery to have access to $.
  2. We\'re getting file and name from inputs with lines below:

  3. We\'re adding action for WordPress and form inputs with their values into FormData:

    Here you can change the name of array we\'re sending. For ex., we will recieve file in the array with key customer-field-upload and can use it such way:

  4. async : true: this line we don\'t need. It\'s true by default. enctype: \'multipart/form-data\', we don\'t need, too.
  5. data: form_data,: we are sending everything only using one form_data variable( FormData object ).

PHP file:

  1. I didn\'t use add_action(\'wp_ajax_nopriv_form_submit1\', \'handle_form_submission\'); because there is nothing related to nonce in your provided code
  2. Your $_POST will contain:

    You can access to name input using $_POST[\'customer-field-name\'].

  3. Your $_FILES will contain:

    You can work with input file using $_FILES[\'customer-field-upload\']


EDIT: Appended functional to add all inputs with several lines of code:

Form:

Ajax.js:

Here we used Jquery iterator to add several values from the loop into the FormData object. This is example and can be applied for checkboxes, textareas and etc.

###

You are missing enctype: \'multipart/form-data\' and contentType: false to prevent jQuery from setting it to string. If that doesn\'t work, see this example and follow the same method

###

Your Jquery is missing the

This is quite important when you are submitting the forms. This allows the form data to be sent without any conversions.

由于网站搬家,部分链接失效,如无法下载,请联系站长!谢谢支持!
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » 使用Ajax提交文件上传的WordPress表单

发表回复

CAPTCHAis initialing...