Reputation: 49384
I have an existing form on my working php page.
<form action="index.php" method="POST">
...
I want to add image upload to it but the script requires enctype?
<form enctype="multipart/form-data" action="index.php" method="POST">
...
Is it possible to just add the enctype="multipart/form-data" to a form that submits other input data which is not images or do I need to have 2 forms on my page?
Upvotes: 1
Views: 387
Reputation: 138041
The enctype
attribute specifies through a MIME type how form data is encoded in the request. The two valid enctypes I know are application/x-www-form-urlencoded
and multipart/form-data
.
Under the hood, when you specify no type, you get application/x-www-form-urlencoded
. This indicates that when sending the request, your browser will encode the form data the same way it would if it were to send it as GET parameters: the request body looks like foo=bar&bar=baz&frob=this%20has%20four%20words
. While perfectly suitable for text fields, this representation is pretty awkward for file transfers, especially for binary files while would likely triple in size. To avoid something like that happening, it's not possible to use application/x-www-form-urlencoded
to send files.
multipart/form-data
, on the other hand, results in a very different representation. I don't remember the exact details, but each input field in your form can have a distinct MIME type and encoding. This means your browser can easily send binary files to a site, along with form data. However, the general structure of the request creates a relatively large overhead, so you should use it only when it's necessary (i.e. when you want to send files).
So, yes, setting enctype="mutlipart/form-data"
will still let you have regular fields too. They just will be represented another way, but that's completely transparent to you.
Upvotes: 1
Reputation: 2177
Adding the attribute enctype="multipart/form-data" will not prevent the form from accepting data from other input types such as text, checkbox or radio. You can submit all your data using a single form.
Upvotes: 3
Reputation: 227220
A form that has file uploads can also post other input fields just fine with enctype="multipart/form-data"
.
Upvotes: 1
Reputation: 3826
You can just add the attribute to form
and the field inside and that will work !
Upvotes: 1