0

I want to get the value of <input type="file" id="imageId"/> using javascript and send it to PHP. I always use jQuery to send data to PHP, but I don't know how to use that on an image without using the form tag. Here is my code:

<html>
  <body>
     <input type="file" id="imageId"/>
     <br>
     <input type="button" value="upload" onclick="getImage()">
  </body>
</html>

function getImage(){
    var image = document.getElementById("imageId").value;
    $.post('backend-process.php', { imageFromJs: image }, function(data) {
        if (data == 0) {
            alert("successfully Uploaded");
        } else{
            alert(data);
        }
    });
}
<?php
    $imagetmp = $_FILE['imageFromJs']['tmp_name'];
    move_upload_files($imagetmp,"uploads/image.png");
?>

Is there any possible way to make it work without using a usual <form action="upload.php" method="post" enctype="multipart/form-data">?

Thanks for the help.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Arjay Bonus
  • 193
  • 3
  • 11
  • 1
    You can upload files with ajax. Just search google. – Daan Jan 16 '15 at 11:16
  • similar question: http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously – Rodion Jan 16 '15 at 11:19
  • welcome to stackoverflow. this site has a few years worth of questions and answers and a few quite good search options. in fact, the site uses algorithms to find related questions. if you look to the right and a bit below your questions, you will see possible related questions, for example http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously?rq=1 which looks like there are a lots of examples for you to try – cypherabe Jan 16 '15 at 11:25

0 Answers0