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.

3
  • 1
    You can upload files with ajax. Just search google. Commented Jan 16, 2015 at 11:16
  • similar question: stackoverflow.com/questions/166221/… Commented Jan 16, 2015 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 stackoverflow.com/questions/166221/… which looks like there are a lots of examples for you to try Commented Jan 16, 2015 at 11:25

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.