0

I have this jquery code to submit the form however it isn't submitting the form. I'm trying to upload images with this jquery I have php written to upload it what is the problem with my code?

$("#loader").hide();
$("#display").hide();
$("#display1").hide();
$("#submit").click(function(){
   $(".form_cont").hide();
   $("#loader").show();          
   $.ajax({
      type: "POST",
      url: "upload.php",
      dataType:"json",
      success: function (response) {
         if(response.status === "success") {
            $("#display").show();
         } else {
            $("#display1").show();
         }
      }
   });
   return false;        
});
dev
  • 3,969
  • 3
  • 24
  • 36
Athar Ahmed
  • 151
  • 10

2 Answers2

1

May be try this it will work.Demo Link

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
    <title>Ajax file upload</title>
</head>

<body>    
<form action="upload.php" method="post" enctype="multipart/form-data" target="workFrame" >
<input type="file" name="file" />
<input type="submit" />
</form>
<iframe id="workFrame" src="about:blank" style="display:none;"></iframe>
<div id="image_disp"></div>

</body>
</html>
<script src="http://code.jquery.com/jquery-1.8.1.min.js" type="text/javascript"></script>
<script>
    $('form').on('submit', function () {
    //check if the form submission is valid, if so just let it submit
    //otherwise you could call `return false;` to stop the submission
});

$('#workFrame').on('load', function () {
            $('#image_disp').html('');
    //get the response from the server
    var response = $(this).contents().find('body').html();
            $('#image_disp').html('<img src="images/'+response+'"/>');
    //you can now access the server response in the `response` variable
    //this is the same as the success callback for a jQuery AJAX request
});

Upload.php

<?php 
  if(isset($_FILES['file']) && $_FILES['file'] != '')
  {
    if(isset($_FILES['file']['name']) && $_FILES['file']['name'] != '')
    {
            $image_name                     = explode(".",$_FILES['file']['name']);
            $image_type                     = array_pop($image_name);
            move_uploaded_file($_FILES['file']['tmp_name'],'images/upload.'.$image_type);
            echo 'upload.'.$image_type;
    }
  }
 ?>
Kathiravan
  • 689
  • 1
  • 7
  • 22
1

File upload are not possible with Ajax try a plugin like http://www.uploadify.com/

Jay Bhatt
  • 5,601
  • 5
  • 40
  • 62
  • thanks for your suggestion but I already tried with uploadify but I already have my own php code just trying to fix my jquery to get it work. – Athar Ahmed Mar 17 '13 at 12:18
  • You can customize the server side upload script and use you own code. http://www.uploadify.com/documentation/uploadify/customizing-the-server-side-upload-script/ – Jay Bhatt Mar 17 '13 at 12:21