Ajax submits the Form and file upload instance code, and ajaxform instance

Source: Internet
Author: User

Ajax submits the Form and file upload instance code, and ajaxform instance

A few days ago, I found some minor problems. When I write a background management page, I need to upload an image. So I uploaded a Json string and an image file using a very common Form;

To upload an image from a Form, you only need to add enctype = 'multipart/form-data' to the <form> label to upload the image;

However, the problem arises. When I submit a Form using Form, the page for submitting the returned value is directly displayed and the original page is refreshed;

In this way, asynchronous Ajax can be used to implement partial refresh;

I don't talk much about the code;

First, html:

<Form id = "form_insert" method = "post"> <table style = "font-size: 13px; margin: 13px auto; "> <tr> <td style =" text-align: right; "> type </td> <td>: <input id =" acttype "style =" width: 150px "class =" easyui-textbox "data-options =" required: true "> </td> </tr> <td colspan =" 2 "style =" height: 13px "> </td> </tr> <td style =" text-align: right; "> name </td> <td>: <input id = "actname" style = "width: 150px" class = "easyui-textbox" data-options = "required: true "> </td> </tr> <td colspan =" 2 "style =" height: 13px "> </td> </tr> <td style =" text-align: right; "> Start time </td> <td>: <input id = "actstarttime" style = "width: 150px" class = "easyui-datetimebox" data-options = "required: true "> </td> </tr> <td colspan =" 2 "style =" height: 13px "> </td> </tr> <td style =" text-align: right; "> end time </td> <td>: <input id = "actendtime" style = "width: 150px" class = "easyui-datetimebox" data-options = "required: true "> </td> </tr> <td colspan =" 2 "style =" height: 13px "> </td> </tr> <td style =" text-align: right; "> Save </td> <td>: <input id = "mem_Province" style = "width: 150px" class = "easyui-combobox" data-options = "required: true "> </td> </tr> <td colspan =" 2 "style =" height: 13px "> </td> </tr> <td style =" text-align: right; "> city </td> <td>: <input id = "mem_City" style = "width: 150px" class = "easyui-combobox" data-options = "required: true "> </td> </tr> <td colspan =" 2 "style =" height: 13px "> </td> </tr> <td style =" text-align: right; "> stores </td> <td>: <input id = "mem_Shop" style = "width: 150px" class = "easyui-combobox" data-options = "required: true "> </td> </tr> <td colspan =" 2 "style =" height: 13px "> </td> </tr> <td style =" text-align: right; "> specific address </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; "> upload images </td> <td>: <input class =" easyui-filebox "name = 'photo' style =" width: 153px "data-options =" required: true, prompt: 'select upload image', buttonText: 'select' "> </td> <input type = 'text' id =" Item "name = 'item' style =" display: none; "> </td> </tr> </table> </form> <div style =" text-align: right; padding: 2px 5px; "> <a id =" sub "class =" easyui-linkbutton "data-options =" iconCls: 'icon-OK '"href =" javascript: void (0) "> Save </a> <a class =" easyui-linkbutton "data-options =" iconCls: 'icon-quxiao' "href =" javascript: void (0) "onclick =" window_open ($ ('# insert_form'), 'close') "> cancel </a> </div>

The above is the html code. For the convenience of copying, css is directly in the tag;

Many may ask why two form forms are written;

This is because, according to the requirements of the backend to receive data, the information is transferred into strings and images;

First, convert the information into a string;

Put it in the second Form, and be careful to find that in the second form, the <input> label style = "display: none" is a hidden label;

It's good that the data obtained through the first form is converted into a string through js and then put it in a hidden tag;

In this way, you can submit the second 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 ('Warning ',' the end time cannot be earlier than the start time! ', '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, // processData: false, contentType: false, error: function (request) {}, success: function (data) {$ ('# box '). datagrid ('reload') ;}}); window_open ($ ('# insert_form'), 'close');} else {$. messager. alert ('Warning ',' information is incomplete! ', 'Error ');}}});

As you can see, I have used the FormData method. To be honest, this is really very useful in html5. You don't need to write enctype = 'multipart/form-data' to upload images ';

The above section describes the example code of Ajax Form submission and file upload. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.