Ajax+formdata+javascript implementation _javascript techniques for non-refreshing form information submission

Source: Internet
Author: User

Principle:

The DOM collects form information, uses Formdata to collect form information quickly, and instantiates form data objects to collect FM form field information at the same time.

var fd = new FORMDATA (FM); Instantiating an Object

alert (FD);

There is a collection of form form field information within the FD object

Ajax delivery Form Information

1. Static Display page code

<! DOCTYPE HTML >  

2.php page Code

Server save attachment name for itself name

Local file------> upload (PHP program processing)------> server

Character set for local file name gb2312

The character set of the PHP program utf-8--->gb2312 (in the program Utf-8 encoded attachment name transcoding to gb2312) server's character set gb2312

<?php
//$_files[' user_pic ' [' Error ']
//0->ok 1-> size exceeds php.ini limit 2-> limit max_file_size 
-> Attachment only uploaded part (incomplete) 4-> did not upload attachment
if ($_files[' user_pic '] [' ERROR ']>0] {
exit (' Upload attachment has problem, there may be no attachment ');
}
The server holds the attachment name for itself
//local file------> upload (PHP program processing)------> Server
//local file name character Set gb2312
//php program's character set Utf-8--->gb2312
//(in the Program Utf-8 encoded attachment name transcoding to gb2312)
/server character Set gb2312
$name = $_files[' user_pic ' [' Name '];
$name = Iconv (' UTF-8 ', ' GB2312 ', $name); $NAME encoding is changed from UTF-8---to--->gb2312
$path = "./upload/";
Attachment upload Logic
//move_uploaded_file (temporary path name, real pathname);
if (Move_uploaded_file ($_files[' user_pic '] [' tmp_name '], $path. $name))
echo "Success";
else
echo "fail";

Here's a look at jquery's implementation of the form form through Ajax without refreshing submission of the instance code.

The code looks like this:

Convert form to Ajax commit
  function Ajaxsubmit (URL,FRM,FN) {
    var datapara=getformjson (frm);
    $.ajax ({
      url:url,
      type: "Post",
      Data:datapara,
      async:false,
      dataType: ' txt ',
      success : FN
    });
  Converts a value in a form to a key value pair
  function Getformjson (frm) {
    var o={};
    var a=$ (frm). Serializearray ();
    $.each (A,function () {
      if (o[this.name]!==undefined) {
        if (!o[this.name].push) {
          o[this.name]=[o[ THIS.name]];
        }
        O[this.name].push (This.value | | '');
      } else{
        O[this.name]=this.value | | '';
      }
    });
    return o;
  }
//
  /Foreground call mode
  function Autosubmitfun () {
     ajaxsubmit ("Autosumitscoreajax.action", $ (' #formId '), function () {});
*/

The above is a small set of ajax+formdata+javascript to introduce the implementation of no refresh form information submitted, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.