Generally, we upload an image using simple PHP without AJAX. But nowadays AJAX image upload is in high demand. Here I will explain to you how to upload an image in PHP using AJAX. Uploading an image via an AJAX function is very easy and very simple to implement on our page.
Here I have used jQuery AJAX to image upload. We are using a form with a file field and submit button to submit the form in AJAX file.
When the user selects a file, at that time script will be executed and send an upload request to the script destination file. And using PHP code we are uploading an image into the specific folder.
HTML Image Upload Form
Here we have a simple HTML form that includes an input type file. When we submit this form AJAX function will call and send the request to the PHP background file and this file will upload the image into a specific folder.
1 2 3 4 5 6 |
<form id="imform" action="uploadimg.php" method="post"> <label>Upload Image File:</label> <input name="testimage" type="file" class="inputFile"> <input type="submit" value="Submit" class="btnSubmit"> </form> |
PHP Image Upload via AJAX Method
Here you can see the AJAX function which is used to send file upload requests by posting the whole form data instance. It uploads our file to the specified image path. Once our file is uploaded successfully, it will show the image which is uploaded in the folder using AJAX. In response, you can append to a particular div also.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<script type="text/javascript"> $(document).ready(function (e){ $("#imform").on('submit',(function(e){ e.preventDefault(); $.ajax({ url: "uploadimg.php", type: "POST", data: new FormData(this), contentType: false, cache: false, processData:false, success: function(data){ $("#targetdiv").html(data); }, error: function(){} }); })); }); </script> |
So, that’s it for today. I hope, you guys like this article and learn to upload an image using AJAX. If our post really seems to be useful then share it with others. Also, subscribe to your newsletter service so that you can receive notifications for new updates and articles.
Thank you for reading this.
You may also like these posts: What is HTML and How HTML works?
Send Push Notification to Android and iOS App Using PHP and Firebase
Hurrah! In the end I got a webpage from where I
be capable of really obtain helpful information regarding my study and
knowledge.
Hi there, I would like to subscribe for this blog to get most up-to-date updates, so where can i do it
please help.
you can subscribe from home page
Good day! Would you mind if I share your blog with my myspace group?
There’s a lot of folks that I think would really appreciate your content.
Please let me know. Many thanks
my web page: tracfone 2022
Sure you can share no problem