PHP+HTML5 How to submit forms and upload images using Formdata objects _php Tutorial

Source: Internet
Author: User

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 ...

  • 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.