I'm trying to upload an image without submit button. My code works fine for submiting without button.
My HTML code
<form name="service_image_form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Add new image <input name="userfile" type="file" id="userfile" onchange="return subForm()" />
</form>
<script>
function subForm() {
document.service_image_form.submit();
};
</script>
But I'm little confused in retriving data at PHP. In php I tried something like this to retrieve data.
if (isset($_POST['service_image_form']))
{
echo "working";
}
Here I'm trying to echo "working" just for confirmation. If my condition works then then I can save my image to server and db. I know its very simple but stumbed here for a while. surfed lots of links, but no idea. Please help me out with this.
I know if I had a submit button with name="submit" then I can retrive like this
if(isset($_POST['submit']))
{
upload code comes here..
}
I dont wan't submit button..
To check if a post request is made:
if($_SERVER['REQUEST_METHOD']=='POST'){
echo "working";
}
Note that uploaded files data will be in the $_FILES
superglobal, not $_POST
, and as mentioned by Fred, you will need to add the enctype attribute on your form
You need to add enctype attribute into your for like this
<form name="myform" method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Add new image <input name="userfile" type="file" id="userfile" onchange="return subForm()" />
</form>
And in your php you get the uploaded files using the $_FILE
<?php
if ($_FILES['userfile']['error'] > 0) {
echo $_FILES['userfile']['error'];
} else {
echo 'Name: ' . $_FILES['userfile']['name'];
echo 'Temp file location in: ' . $_FILES['userfile']['tmp_name'];
}
?>
So basically you are trying to check if upload is correct, right? Well, it's not how it's done.
PHP has great manual for that http://php.net/manual/pl/features.file-upload.php
You miss:
After that you are good to go, you can find all the details here http://php.net/manual/pl/features.file-upload.post-method.php it's pretty straightforward.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.