php+html5 uses the FormData object to submit forms and upload images,
The example in this article describes how php+html5 uses the FormData object to submit forms and upload images. Share it with everyone for your reference. The specific analysis is as follows:
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
Copy code The code is as follows:
var formdata = new FormData();
formdata.append('name','fdipzone');
formdata.append('gender','male');
2. Obtain the form object and pass it into the FormData object as a parameter
Copy code The code is as follows:
Copy code The code is as follows:
var form = document.getElementById('form1');
var formdata = new FormData(form);
Use FormData to submit forms and upload files:
Copy code The code is as follows:
server.php如下:
复制代码 代码如下:
$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.
http://www.bkjia.com/PHPjc/957143.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/957143.htmlTechArticlephp+html5 uses the FormData object to submit forms and upload images. This article describes how php+html5 uses the FormData object. How to submit forms and upload images. Share it with everyone for everyone...