Home > Backend Development > PHP Tutorial > How to use FormData object to submit forms and upload images in php+html5_PHP tutorial

How to use FormData object to submit forms and upload images in php+html5_PHP tutorial

WBOY
Release: 2016-07-13 10:07:10
Original
889 people have browsed it

php+html5使用FormData对象提交表单及上传图片的方法

 这篇文章主要介绍了php+html5使用FormData对象提交表单及上传图片的方法,实例分析了FormData对象的使用技巧,非常具有实用价值,需要的朋友可以参考下

 

 

本文实例讲述了php+html5使用FormData对象提交表单及上传图片的方法。分享给大家供大家参考。具体分析如下:

FormData 对象,可以把form中所有表单元素的name与value组成一个queryString,提交到后台。在使用Ajax提交时,使用FormData对象可以减少拼接queryString的工作量。

使用FormData对象

1.创建一个FormData空对象,然后使用append方法添加key/value

代码如下:

var formdata = new FormData();
formdata.append('name','fdipzone');
formdata.append('gender','male');

 

2.取得form对象,作为参数传入到FormData对象

代码如下:





代码如下:

var form = document.getElementById('form1');
var formdata = new FormData(form);

 

使用FormData提交表单及上传文件:

 

代码如下:





FormData Demo





name:


gender:male female


photo:






server.php is as follows:

The code is as follows:

$name = isset($_POST['name'])? $_POST['name'] : '';
$gender = isset($_POST['gender'])? $_POST['gender'] : '';
$filename = time().substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'],'.'));
$response = array();
if(move_uploaded_file($_FILES['photo']['tmp_name'], $filename)){
$response['isSuccess'] = true;
$response['name'] = $name;
$response['gender'] = $gender;
$response['photo'] = $filename;
}else{
$response['isSuccess'] = false;
}
echo json_encode($response);
?>

The operation effect is as shown below:

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/956980.htmlTechArticlephp+html5 uses the FormData object to submit forms and upload images. This article mainly introduces the use of FormData in php+html5 Methods for object submission forms and uploading images, example analysis of FormDa...
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