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

Ajax form submission and file upload example code

韦小宝
Release: 2018-05-14 15:09:53
Original
3021 people have browsed it

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

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:

<form id = "form_insert" method = "post">
<table style = "font-size: 13px; margin: 13px auto;"> 
<tr>
<td style = "text-align: right;">类型</td>
<td>:  <input id = "acttype" style = "width:150px" class = "easyui-textbox" data-options = "required:true"></td>
</tr>
<tr><td colspan = "2" style = "height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">名称</td>
<td>:  <input id = "actname" style = "width:150px" class = "easyui-textbox" data-options = "required:true"></td>
</tr>
<tr><td colspan = "2" style = "height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">开始时间</td>
<td>:  <input id = "actstarttime" style = "width:150px" class = "easyui-datetimebox" data-options = "required:true"></td>
</tr>
<tr><td colspan = "2" style = "height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">结束时间</td>
<td>:  <input id = "actendtime" style = "width:150px" class = "easyui-datetimebox" data-options = "required:true"></td>
</tr>
<tr><td colspan = "2" style = "height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">省</td>
<td>:  <input id ="mem_Province" style = "width:150px" class = "easyui-combobox" data-options = "required:true"></td>
</tr>
<tr><td colspan="2" style="height: 13px"></td>
</tr>
<tr>
<td style="text-align: right;">市</td>
<td>:  <input id = "mem_City" style = "width:150px" class = "easyui-combobox" data-options = "required:true"></td>
</tr>
<tr><td colspan = "2" style = "height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">门店</td>
<td>:  <input id = "mem_Shop" style = "width:150px" class = "easyui-combobox" data-options = "required:true"></td>
</tr>
<tr><td colspan="2" style="height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">具体地址</td>
<td>:  <input id = "actadd" style = "width:150px" class = "easyui-textbox" data-options = "required:true"></td>
</tr>
</table>
</form>
<form id = "form_sub" style = "font-size: 13px;">
<table style="font-size: 13px; margin: 13px auto;">
<tr>
<td style = "text-align: right;">上传图片</td>
<td>:  <input class = "easyui-filebox" name = &#39;photo&#39; style = "width:153px" data-options = "required:true,prompt:&#39;选择上传图片&#39;,buttonText:&#39; 选 择 &#39;"></td>
<td><input type = &#39;text&#39; id = "Item" name = &#39;item&#39; style = "display:none;"></td>
</tr>
</table>
</form>
<p style = "text-align:right; padding:2px 5px;">
<a id = "sub" class = "easyui-linkbutton" data-options = "iconCls:&#39;icon-ok&#39;" href = "javascript:void(0)">
保存
</a>    
<a class = "easyui-linkbutton" data-options = "iconCls:&#39;icon-quxiao&#39;" href = "javascript:void(0)" onclick = "window_open($(&#39;#insert_form&#39;), &#39;close&#39;)">
取消
</a>    
</p>
Copy after login

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 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:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template