-1

How can I replace old image's path with new image. I'm using ajax and js to upload image using php. I want to replace this no-image.jpg with new uploaded image's path and also want to save image's path to database, so far what I did is My index

   <html>
   <head>
   <link rel="stylesheet" href="style.css" type="text/css"/>
   <script src="js/jquery-1.11.3-jquery.min.js"></script>      
   <script type="text/javascript" src="js/script.js"></script>
   </head>
   <body>
   <div class="container">
       <div id="preview"><img src="no-image.jpg" /></div>
       <form id="form" action="ajaxupload.php" method="post"   enctype="multipart/form-data">
        <input id="uploadImage" type="file" accept="image/*" name="image" />
        <input id="button" type="submit" value="Upload">
       </form>
   </div>
   </body>
   </html>

My ajaxupload.php

<?php
    $valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp'); // valid      extensions
    $path = 'uploads/'; // upload directory

    if(isset($_FILES['image']))
    {
        $img = $_FILES['image']['name'];
        $tmp = $_FILES['image']['tmp_name'];

        // get uploaded file's extension
        $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));

       // can upload same image using rand function
       $final_image = rand(1000,1000000).$img;

       // check's valid format
       if(in_array($ext, $valid_extensions)) 
       {                    
          $path = $path.strtolower($final_image);   
          if(move_uploaded_file($tmp,$path)) 
          {
              echo "<img src='$path' />";
          }
       } 
       else 
       {
           echo 'invalid';
       }
    }
?>

And my script.js

  $(document).ready(function (e) 
  {
      $("#form").on('submit',(function(e) 
      {
          e.preventDefault();
          $.ajax({
                  url: "ajaxupload.php",
                  type: "POST",
                  data:  new FormData(this),
                  contentType: false,
                  cache: false,
                  processData:false,
                  beforeSend : function()
                  {
                      //$("#preview").fadeOut();
                      $("#err").fadeOut();
                  },
              success: function(data)
              {
              if(data=='invalid')
              {
                // invalid file format.
                $("#err").html("Invalid File !").fadeIn();
              }
              else
              {
                // view uploaded file.
                $("#preview").html(data).fadeIn();
                $("#form")[0].reset();  
             }
         },
        error: function(e) 
        {
            $("#err").html(e).fadeIn();
        }           
   });
}));

});

Stuti Rauthan
  • 51
  • 1
  • 1
  • 7
  • Possible duplicate of [Changing the image source using jQuery](http://stackoverflow.com/questions/554273/changing-the-image-source-using-jquery) – Iceman Jan 23 '17 at 11:29

1 Answers1

0

On server side:

echo $path;

In ajax success function:

$('#preview img').attr('src',data)