Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How send input type file to image upload file using post method in javascript

How send input type file to image upload file using post method in JavaScript? Blew is the form.

<form action="#" method="post" enctype="multipart/form-data" id="image_upload" name="image_upload">
    <input type="file" name="myFile" id="myFile" onchange="readURL(this);" required>
    <br>
    <img id="blah" src="#" alt="your image" />
    <input type="hidden" name="user_id" id="user_id" value="<?php echo $user_id; ?>">
    <input type="submit" value="Upload">
</form>

Here blow is JavaScript that I have used for post the Form.

$(document).ready(function(){ 
$('#image_upload').submit(function(e){
        e.preventDefault();

        $.post('getfile_provider.php',$('#image_upload').serialize(),function(data){
            alert(data);
        });

    });
}); 

When i send in this method i get in getfile_provider.php file only the user_id value.

like image 276
sanji Avatar asked Jun 03 '26 11:06

sanji


1 Answers

You can use this to sending image file using post method

 var formData = new FormData($("#form")[0]);
            $.ajax({
                url: "getfile_provider.php",
                type: 'POST',
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
            })
            .done(function(data) {
                alert('done');
            });
like image 61
Saty Avatar answered Jun 06 '26 00:06

Saty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!