PHP+HTML5 use the Formdata object to submit the form and upload the image method,
This article describes how PHP+HTML5 uses Formdata objects to submit forms and upload pictures. Share to everyone for your reference. The specific analysis is as follows:
FormData object, you can put the name and value of all form elements in a form into a querystring, submitted to the background. When using AJAX submissions, using Formdata objects can reduce the effort of stitching querystring.
Using the Formdata object
1. Create a Formdata empty object, and then use the Append method to add the Key/value
Copy the Code code as follows: var formdata = new Formdata ();
Formdata.append (' name ', ' Fdipzone ');
Formdata.append (' Gender ', ' Male ');
2. Get the Form object and pass it as a parameter to the Formdata object
Copy the Code code as follows:
Copy the Code code as follows: var form = document.getElementById (' Form1 ');
var formdata = new Formdata (form);
Use Formdata to submit forms and upload files:
Copy the Code code as follows:
FormData Demo
server.php as follows:
Copy the Code code as follows: <?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 effect is as follows:
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/957143.html www.bkjia.com true http://www.bkjia.com/PHPjc/957143.html techarticle PHP+HTML5 using Formdata object to submit the form and upload the image method, this article describes the PHP+HTML5 use Formdata object submission form and upload pictures method. Share to everyone ...