Home > Web Front-end > HTML Tutorial > How to submit forms and upload images using FormData

How to submit forms and upload images using FormData

一个新手
Release: 2017-09-14 10:52:14
Original
2363 people have browsed it

FormData object can combine the name and value of all form elements in the form into a queryString and submit it to the background. When submitting using Ajax, using the FormData object can reduce the workload of splicing queryString.


Use FormData object

1. Create an empty FormData object, and then use the append method to add key/value


  1. ##var formdata = new FormData();

  2. ##formdata.append(

    'name',' fdipzone');

  3. ##formdata.append(
  4. 'gender'

    ,'male');

    2. Obtain the form object and pass it into the FormData object as a parameter
    <form name="form1" id="form1">  
    <input type="text" name="name" value="fdipzone">  
    <input type="text" name="gender" value="male">  
    </form>
Copy after login



##

var form = document.getElementById(&#39;form1&#39;);  
var formdata = new FormData(form);
Copy after login

  1. Use FormData to submit forms and upload files:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
<html>  
 <head>  
  <meta http-equiv="content-type" content="text/html; charset=utf-8">  
  <title> FormData Demo </title>  
  <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>  
  
  <script type="text/javascript">  
  <!--  
    function fsubmit(){  
        var data = new FormData($(&#39;#form1&#39;)[0]);  
        $.ajax({  
            url: &#39;server.php&#39;,  
            type: &#39;POST&#39;,  
            data: data,  
            dataType: &#39;JSON&#39;,  
            cache: false,  
            processData: false,  
            contentType: false  
        }).done(function(ret){  
            if(ret[&#39;isSuccess&#39;]){  
                var result = &#39;&#39;;  
                result += &#39;name=&#39; + ret[&#39;name&#39;] + &#39;<br>&#39;;  
                result += &#39;gender=&#39; + ret[&#39;gender&#39;] + &#39;<br>&#39;;  
                result += &#39;<img src="&#39; + ret[&#39;photo&#39;]  + &#39;" width="100">&#39;;  
                $(&#39;#result&#39;).html(result);  
            }else{  
                alert(&#39;提交失敗&#39;);  
            }  
        });  
        return false;  
    }  
  -->  
  </script>  
  
 </head>  
  
 <body>  
    <form name="form1" id="form1">  
        <p>name:<input type="text" name="name" ></p>  
        <p>gender:<input type="radio" name="gender" value="1">male <input type="radio" name="gender" value="2">female</p>  
        <p>photo:<input type="file" name="photo" id="photo"></p>  
        <p><input type="button" name="b1" value="submit" onclick="fsubmit()"></p>  
    </form>  
    <p id="result"></p>  
 </body>  
</html>
Copy after login

  1. server.php

<?php  
$name = isset($_POST[&#39;name&#39;])? $_POST[&#39;name&#39;] : &#39;&#39;;  
$gender = isset($_POST[&#39;gender&#39;])? $_POST[&#39;gender&#39;] : &#39;&#39;;  
  
$filename = time().substr($_FILES[&#39;photo&#39;][&#39;name&#39;], strrpos($_FILES[&#39;photo&#39;][&#39;name&#39;],&#39;.&#39;));  
  
$response = array();  
  
if(move_uploaded_file($_FILES[&#39;photo&#39;][&#39;tmp_name&#39;], $filename)){  
    $response[&#39;isSuccess&#39;] = true;  
    $response[&#39;name&#39;] = $name;  
    $response[&#39;gender&#39;] = $gender;  
    $response[&#39;photo&#39;] = $filename;  
}else{  
    $response[&#39;isSuccess&#39;] = false;  
}  
  
echo json_encode($response);  
?>
Copy after login

The above is the detailed content of How to submit forms and upload images using FormData. 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