upload a file form - doesn't recognize the $_FILES variable - php

I've got the following form:
<form method="post" action="index.php">
product name:
<input type="text" name="product_name" value="<?php echo $product_name;?>"/>
<br /> <br />
product details
<textarea rows = "6" cols = "30" name="product_details" > <?php echo $product_details;?></textarea>
<br /> <br />
product price
<input type="text" name = "product_price" value="<?php echo $product_price;?>"/>
<br /> <br />
CN:
<input type="text" name = "product_cn" value="<?php echo $product_cn;?>"/>
<br /> <br />
image
<input type="file" name="fileField" />
<br /> <br />
<input type="submit" name="submit" value="register product" />
</form>
my problem is, whenever i try to process the image using this code:
move_uploaded_file($_FILES['fileField']['tmp_name'], "../product_images/$newname");
I get the following error:
Notice: Undefined index: fileField
why is that?
Thanks in advance!

You have to add enctype='multipart/form-data' to the form
Quote from this topic about this
When you make a POST request, you have to encode the data that forms the body of the request in some way.
HTML forms provide two methods of encoding. The default is
application/x-www-form-urlencoded, which is more or less the same as a
query string on the end of the URL. The other, multipart/form-data, is
a more complicated encoding but one which allows entire files to be
included in the data.

enctype="multipart/form-data"
Add that attribute to your form tag
<form method="post" action="index.php" enctype="multipart/form-data">
multipart/form-data No characters are encoded. This value is required
when you are using forms that have a file upload control
http://www.w3schools.com/tags/att_form_enctype.asp

Related

How to post html outside your domain?

Let's say we are making a form that we want to have on domain1.com and we want to post it to domain2.com. Can this be done?
<form method="post" action="domain2.com/receivepost.php">
<input type="text" name"text" />
<input type="submit value="submit" />
</form>
domain2.com/receivepost.php
<?php print_r($_POST); ?>
You need to specify the full URL (including protocol) in the action attribute of your form tag as follows:
<form method="post" action="http://domain2.com/receivepost.php">
<input type="text" name="text" />
<input type="submit" value="submit" />
</form>
On the PHP side, you can check for POST data as follows:
if (isset($_POST['text'])) {
echo $_POST['text'];
}
Notice that the name attribute of the input tag is text.
You need to specify full URL if you want to send request to specific domain
<form method="post" action="http://domain2.com/receivepost.php">
<input type="text" name"text" />
<input type="submit value="submit" />
</form>
Or you can send to relative (for your domain) path:
<form method="post" action="receivepost.php">
<input type="text" name"text" />
<input type="submit value="submit" />
</form>

why my forms are not working in bootstrap website?

I tried all but no form is working with method or simple action like http://google.com `
<form action="http://google.com" method="post">
<input type="text" />
<input type="submit" />
</form>
even this is not working
use this
<form role="form" action="http://google.com" method="post">
<input type="text" name="text" />
<input type="submit" name="submit" />
</form>
role attribute for FORM
name attribute for input
In PHP, $_POST always accept name attribute from form elements:-
You need to write
<input type="text" name='name_of_your_field' />
instead of
<input type="text" />
You need to refer this Link.

POST array is empty when containing a file

I have a simple HTML form containing a file input. When the form is submitted without a file, printing the $_POST array shows me all of the data submitted. When a file is submitted, however, $_POST doesn't print out any of the submitted data.
Can somebody tell me why? This is my code:
<?php
print_r($_POST);
?>
<form action="test.php" method="post" enctype="multipart/form-data">
<label for="myfile">Video File:</label>
<input type="file" name="myfile" />
<br /><br />
<label for="mytitle">Title:</label><br />
<input type="text" name="mytitle" size="55" maxlength="60" />
<br /><br />
<input type="submit" name="mysubmit" value="Submit Video for Approval" />
</form>
Your script seems fine. Please check your server configuration. Perhaps you exceed POST limits (set with post_max_size in php.ini)
You have to use $_FILES to access the uploaded files.
var_dump($_FILES); // Your uploaded files
var_dump($_POST); // Your entered data

Tomcat 6 & PHP with enctype - No input data returned

I have this problem when I'm using PHP5/HTML on Apache-Tomcat6.
Here's an example for one of the forms I use in my site:
<form enctype="multipart/form-data" method="post" action="hello.php" >
<label>Title* :</label>
<input type="text" name="title" />
<label>Image:</label>
<input type="file" name="image" /><br />
<input type="submit" value="Add"/>
</form>
Whenever I add the 'enctype' attribute to any form; neither the $_FILES['image'] is returned nor the $_POST variables. As long as the 'enctype' is not there, everything (except for the file input of course) works as expected. Can any one guide me please?
You won't be able to post data with a method of get on your form.
In test.html:
<form enctype="multipart/form-data" method="post" action="hello.php" >
<label>Title* :</label>
<input type="text" name="title" />
<label>Image:</label>
<input type="file" name="image" /><br />
<input type="submit" value="Add"/>
</form>
In hello.php:
<?php
print_r($_POST);
print_r($_FILES);
Depending upon your server configuration, this will combine $_GET, $_POST, and $_COOKIE, but you'll still want to post with file inputs.
print_r($_REQUEST);

How to Combine Post and Upload in HTML

<body>
<form method="post" action="xx.php" >
Enter Title of the Post<INPUT type="text" name="title">
<br/>
Enter Description
<textarea rows="10" cols="50" wrap="physical" name="post">
</textarea><br/>
<input type="Submit" value="Post">
<br/><br/>
<form enctype="multipart/form-data" action="xx.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload: <input name="uploaded_file" type="file" />
<input type="submit" value="Upload" />
</form>
How Do I get the user to write the text and browse the image, only after which pressing a single button would both upload the text and the file?
You can't submit two forms with the same button. You'll need to combine the two fields into a single form.
You can move the file input to the first <form> (which btw, you haven't closed) and use javascript to check if text has been entered and file has been selected.
Don't nest your form tags (It's invalid HTML). You can place all the inputs in one form so they are posted together.
e.g.
<form enctype="multipart/form-data" action="xx.php" method="post">
Enter Title of the Post<INPUT type="text" name="title">
<br/>
Enter Description
<textarea rows="10" cols="50" wrap="physical" name="post">
</textarea>
<br/>
<br/>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload: <input name="uploaded_file" type="file" />
<input type="submit" value="Upload" />
</form>
Oh, as a sidenote, you may not want to use a name like "post" in your input control.

Categories