Home  >  Article  >  Web Front-end  >  Ajax form submission and file upload example code

Ajax form submission and file upload example code

韦小宝
韦小宝Original
2018-05-14 15:09:532915browse

I found a small problem when I was doing the program some time ago. When I was writing the background management Form form to upload pictures, when I submitted it using the Form form, it jumped out and submitted it directly. The page that returns the value and the original page is refreshed. The following editor will share with you the example code of Ajax submission of Form form and file upload

a few days ago , found some minor problems. When I was writing the background management page, I needed to upload a picture. So I used a very common Form to upload a Json string and image files;

Form form to upload images, you only need to add enctype = 'multipart/form-data' in the ff9c23ada1bcecdd1a0fb5d5a0f18437 tag, like this You can upload pictures;

But here comes the problem. When I submit the form using the Form, the page that submits the return value will pop up directly and the original page will be refreshed;

In this way, we can arrive first Asynchronous Ajax can achieve partial refresh;

Without further ado, let’s go directly to the code;

First is the html:

类型 :
名称 :
开始时间 :
结束时间 :
:
:
门店 :
具体地址 :
上传图片 :

保存 取消

The above is the html code, for the convenience of everyone to copy, The css is directly in the tag;

Many friends want to ask why two form forms are written;

This is because according to the need to receive data in the background, the information transmitted becomes String and picture;

First turn the information into a string;

and then put it in the second Form form. A careful friend found that in the second form form The style="display:none" in the d5fd7aea971a85678ba271703566ebfd tag is a hidden tag;

Yes, I got the data through the first form and turned it into a string through js and then put it in the hidden tag. ;

In this way, you can submit the second Form form through Ajax;

js code:

$( '#sub' ).click( function () {
  var actTimeStart1 = $ ('#actstarttime') . datebox ('getValue');
  var actTimeStart = changeDateToLong(actTimeStart1);
  var actTimeEnd1 = $('#actendtime').datebox('getValue');
  var actTimeEnd = changeDateToLong(actTimeEnd1);
  if(actTimeStart != '' && actTimeEnd != '' && (actTimeStart - actTimeEnd > 0)){
    $.messager.alert('警告','结束时间不能小于开始时间!','error');
    return false;
  }
  else{
    if ($('#form_insert').form('validate')) {
      var actType = document.getElementById("acttype").value;
      var actName = document.getElementById("actname").value;
      var actArea = document.getElementById("actadd").value;
      var actTimeStart1 = $('#actstarttime').datebox('getValue');
      var actTimeStart = changeDateToLong(actTimeStart1);
      var actTimeEnd1 = $('#actendtime').datebox('getValue');
      var actTimeEnd = changeDateToLong(actTimeEnd1);
      var t2 = $('#mem_Shop').combobox('getValue');
      var jsonObj = {actType:actType,actName:actName,actTimeStart:actTimeStart,actTimeEnd:actTimeEnd,actArea:actArea,t2:t2};
      var activityMemberJson = JSON.stringify(jsonObj);
      document.getElementById("Item").value=activityMemberJson;
      var form = new FormData(document.getElementById("form_sub"));
      $.ajax({
        url : ../activity/actionActivityInsert', //http://www.cnblogs.com/jayxxxxxxx/
        type : "post",
        data : form, //第二个Form表单的内容
        processData : false,
        contentType : false,
        error : function(request) {
        },
        success : function(data) {
          $('#box').datagrid('reload');
        }
      });
      window_open($('#insert_form'), 'close');
    }else {
      $.messager.alert('警告' , '信息不完整!' , 'error');
    }
  }
});

Everyone has seen that I used the FormData method. To be honest, this is HTML5 is really easy to use. You don’t need to write enctype = 'multipart/form-data' when uploading images;

The above is the example of Ajax form submission and file upload introduced by the editor. Code, hope it helps everyone!

Related recommendations:

Use the FormData object of html5 to asynchronously submit file data through the Ajax form

JQuery Create PHP AJAX form submission example

Implement Ajax form verification example

The above is the detailed content of Ajax form submission and file upload example code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn