How to Upload & Display Image Using jQuery AJAX PHP and MySQL

How to Upload Image Using jQuery AJAX PHP and MySQL
Hello Friends, In this article we will learn how to upload & display image using jQuery AJAX in PHP MySQL. uploading images using the AJAX function is easy and simple to implement on your web page.
I use jQuery AJAX to implement image uploads. There is a form with fields for file input and buttons to upload. When a form with the selected image file is sent, the AJAX script is run. In this code, a PHP upload request is sent with the uploaded image. The PHP code moves the uploaded image to the uploads folder and returns the HTML image to display the preview in AJAX response.

Create HTML Image Upload Form

<!Doctype html>
<html lang="en">
  <meta charset="utf-8">
  <title>How to Upload & Display Image Using jQuery AJAX PHP and MySQL</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></script>
  <body style="margin-top:50px;background:#f2f2f2">
    <div class="container">
      <h2 style="text-align:center;">How to Upload & Display Image Using jQuery AJAX PHP and MySQL</h2>
        <div class="row">
         <div class="col-md-4"></div>  
          <div class="col-md-4" style="margin-top:20px;margin-bottom:20px;">

          <div class="alert alert-success alert-dismissible success" style="display: none;">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <span class="success-message">File uploaded successfully</span>  
          <div class="alert alert-danger alert-dismissible danger" style="display: none;">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <span class="danger-message">This type of image is not allow</span>  

          <form id="submitForm">
            <div class="form-group">
              <label for="file">Select File</label>
              <input type="file" class="form-control" name="file" id="image" required="">
            <div class="form-group">
              <button type="submit" class="btn btn-success btn btn-block">Upload</button>
      <div class="row">
      <div class="col-md-4"></div>  
        <div class="card col-md-4" id="preview" style="display: none;">
          <div class="card-body" id="imageView">

Upload Image to use jQuery AJAX

This code shows the ajax () function used to send file upload requests by publishing the FormData instance. In PHP the file is uploaded in the specified path. After successfully uploading the image, the image HTML code will be printed as an answer as AJAX. This is then added to the target target to show the preview user.
jQuery AJAX Script to use after </html> in index.php
<!---jQuery ajax file upload --->
<script type="text/javascript">
      $("#submitForm").on("submit", function(e){
        var formData = new FormData(this);
          url  : "upload.php",
          type : "POST",
          data :formData,
          contentType : false, // you can also use multipart/form-data replace of false
          processData: false,

Create PHP file to upload and show Image


	// upload file using move_uploaded_file function in php
	if (!empty($_FILES['file']['name'])) {

		$fileName = $_FILES['file']['name'];
		$fileExt = explode('.', $fileName);
		$fileActExt = strtolower(end($fileExt));
		$allowImg = array('png','jpeg','jpg');
		$fileNew = rand() . "." . $fileActExt;  // rand function create the rand number 
		$filePath = 'uploads/'.$fileNew; 

		if (in_array($fileActExt, $allowImg)) {
		    if ($_FILES['file']['size'] > 0  && $_FILES['file']['error']==0) {  
			if (move_uploaded_file($_FILES['file']['tmp_name'], $filePath)) {
		    	    echo '<img src="'.$filePath.'" style="width:320px;height:300px;"/>';
			    echo "File is not uploaded try again";
		    	    echo "Unable to upload physical file";
		    echo "This type of image is not allow";



You can always support by sharing on social media or recommending my blog to your friends and colleagues.
If you have any suggestions / problems about this tutorial, please comment on the  form below.😊

Leave a Reply

Your email address will not be published.