0

I want to return a temp file name from php using ajax but the code i have tried so far doesn't working. Here is the code:-

   <!DOCTYPE html>
   <html>
      <head>
    <script>
    function getValue(){
        var file=document.getElementById("file").value;
        var xhr;
        if(window.XMLHttpRequest){
            xhr=new XMLHttpRequest();
        }
        url="getImageValue.php?data="+file;
        xhr.open("GET",url,false);
        xhr.onreadystatechange=function(){
            if(xhr.readyState==4 && xhr.status==200){
                document.getElementById("feedback").innerHTML=xhr.responseText;
            }
        }
        xhr.send(null);
    }
    </script>
    </head>
    <body>

       <input type="file" id="file" name="file" />
       <input type="button" id="submit" name="submit" value="catch" onClick="getValue()" />

      <div id="feedback"></div>
    </body>
   </html>

getImageValue.php

<?php
     echo basename($_GET["data"]);
?>

Please help.. Jason

Vipin
  • 153
  • 7
  • 21
  • code is working check url="getImageValue.php?data="+file; have you correct path of file – Rakesh Sharma May 02 '14 at 06:37
  • Your coding working well for just show upload image name but it doesn't upload image to server. – Manibharathi May 02 '14 at 06:39
  • Yup code is working and its returning the name of file but what i want is that it must return a tmp file name like a $_FILES["name of file"]["tmp_name"] returns. – Vipin May 02 '14 at 06:40
  • @Manibharathi I know it doesnt uploading to the server coz i haven't got any temp_name from a file. – Vipin May 02 '14 at 06:43

1 Answers1

1

Use form tag with enctype="multipart/form-data" attribute.

<form enctype="multipart/form-data" action="/upload/image" method="post">

Ajax using file upload

jQuery Ajax File Upload

Community
  • 1
  • 1
Manibharathi
  • 945
  • 6
  • 18