0

I have below html

This form allows you to upload a file to the server.<br>
        <div class="imguploadpath" style="width:100%;height:200px;background-color:#CCC;">
          <form name="myFormName" id ="myFormId" action="" method="post">
             <input type="file" name="img" id = "image_pe">
             <br/><br/><br/><br/>
             <input id="cmdSubmit" value="Submit" type="submit" class="">
          </form>
        </div>

I am trying to get the file field values to upload the file in php using below script

jQuery( "#cmdSubmit" ).on( "click", function() {
   var file = jQuery('#image_pe').val(); 

//i have to send file property to php file in jquery ajax

 $.ajax({
            url: 'submit.php?files',
            type: 'POST',
            data: data,
            cache: false,
            dataType: 'json',
            processData: false, // Don't process the files
            contentType: false, // Set content type to false as jQuery will tell the server its a query string request
            success: function(data, textStatus, jqXHR)
            {
                if(typeof data.error === 'undefined')
                {
                    // Success so call function to process the form
                    submitForm(event, data);
                }
                else
                {
                    // Handle errors here
                    console.log('ERRORS: ' + data.error);
                }
            },
            error: function(jqXHR, textStatus, errorThrown)
            {
                // Handle errors here
                console.log('ERRORS: ' + textStatus);
                // STOP LOADING SPINNER
            }
        });
       return false;

    });

but var file only return some temp path c://fakepath/image.jpg

how do upload in jquery and php, I need to pass the form field in php file, I know value() jquery function is wrong.

what is the correct way to do it ?

for php file upload i was thinking of using this script http://www.tizag.com/phpT/fileupload.php

Hitesh
  • 4,098
  • 11
  • 44
  • 82
  • Maybe this will help [http://stackoverflow.com/questions/24168040/upload-multiple-files-with-php-and-jquery/24168617#24168617][1] [1]: http://stackoverflow.com/questions/24168040/upload-multiple-files-with-php-and-jquery/24168617#24168617 Hope it helps – morne Aug 14 '14 at 06:00
  • http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax .... this also helped me a lot to find the problem – Hitesh Aug 14 '14 at 09:16

2 Answers2

1

Try this: JQuery:

var formdata = new FormData();
            jQuery.each($('#image_pe')[0].files, function(i, file) {
             formdata.append('image_pe', file);
            });


            $.ajax({
                url: "/my.php",
                data : formdata,
                dataType : "json",
                type : "post",
                cache: false,
                contentType: false,
                processData: false,
                success: function(data){

                },
                failure: function(){
                    $(this).addClass("error");
                }
            });
            return false;

PHP Code:

$file = $_FILES['image_pe'];
SNAG
  • 2,011
  • 2
  • 23
  • 43
0

Check this. This is might be very helpful for you

http://malsup.com/jquery/form/#file-upload

THAS
  • 9
  • 2