1

My last question was related to a bad reloading of a PHP page after uploading a file.

Now I have to reload just a div instead of the entire page, and JQuery is not my best friend..

I put the form into a div as follows, with the following JQuery function (I got the JQuery code from here):

<div id="updiv">
<form id="uploadFile" action="" method="post" enctype="multipart/form-data">
<h3>Upload a file:</h3>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" id="submit" value="Upload" name="submit">
</form>
</div>

<script>
$('#uploadFile').submit(function()
{
  event.preventDefault();
  $.ajax(
  {
    url: "upload.php",
    type: 'POST',
    data: $(this).serialize(),
    success: function(data)
    {
      $("#updiv").fadeOut();
      setTimeout(1000);
      $("#updiv").fadeIn();
    });
  });

  $(this).ajaxSubmit(options);
      return false;
});
</script>

And this is the upload.php file:

<?php
/* upload.php */
set_time_limit(0);

$targetDir = "/path/to/upload/dir";
$targetFile = $targetDir . basename($_FILES["fileToUpload"]["name"]);
$upload = 1;
$fileType = pathinfo($targetFile, PATHINFO_EXTENSION);

if(isset($_POST["submit"]))
{
    /* Check file size */
    if($_FILES["fileToUpload"]["size"] > 500000)
    {
        echo "Sorry, your file is too large.";
        $upload = 0;
    }
    /* Allow certain file formats */
    if($fileType != "data" )
    {
        echo "Sorry, non valid filetype.";
        $upload = 0;   
    }
    /* Check if $uploadOk is set to 0 by an error */
    if($upload == 0)
    {
        echo "Sorry, your file was not uploaded.";
    } 
    else
    {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile))
        {
            echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        }
        else
        {
            echo "Sorry, there was an error uploading your file.";
        }
    }
}

If I change the form's action attribute as:

<form id="uploadFile" action="upload.php" method="post" enctype="multipart/form-data">
<h3>Upload a file:</h3>
<input type="file" name="fileToUpload" id="fileToUpload" required>
<input type="submit" id="submit" value="Upload" name="submit">
</form>

I can upload the file but nothing is reloaded as I want, and I think it's obvious..

Otherwise, with the code posted above, the browser returns the error:

501 Not Implemented

The requested method is not recognized

From here I see that this is a web server problem.. The web server is httpd.

How can I solve my problem ? Thank you in advance.

Community
  • 1
  • 1
  • 1
    Just to be sure, why are you including `"fileform.php"`in every if statement? Is there a reason for that? – theLibertine Dec 11 '14 at 12:30
  • Ouch! This is an error.. I correct. Sorry. I copied the content of **upload.php** from the linked thread. But the original file running on the web site does not have these instructions.. Sorry again I correct them. –  Dec 11 '14 at 12:34
  • Ok, what if you put `include("fileform.php);` on top? – theLibertine Dec 11 '14 at 12:40

3 Answers3

1

I suggest you to use blueimp jQuery File Upload

there is a demo php project that you can get the idea from it and here you can learn how to implement it.
and about reload a div part I have no idea what do you mean by that, can you please explain it more?

update:
the output of your upload.php file could be loaded into updiv. in example if you do die('blabla'); in upload.php you can use it as updiv inner html. if you used the jQuery plugin that I mentioned above it would be like this:

<script>
$(function () {
    $('#fileupload').fileupload({
        dataType: 'html', // this could be json too, depends on your needs
        done: function (e, data) { // data.result is whatever returned from your upload.php
            $.each(data.result.files, function (index, file) {
                $('#updiv').html(data.result);
            });
        }
    });
});
</script>
mhesabi
  • 1,140
  • 3
  • 22
  • 48
  • I don't want to reload the entire page. I just want to update the content of the div **updiv** which contains the form. I am looking at your link. Thank you in advance. I hope it will help. –  Dec 11 '14 at 12:51
1

This is a working example you have to use formData to be able to send files via ajax, this will reload the div and return the server response in <div class="info"></div>

  <form id="uploadFile" enctype="multipart/form-data">
    <h3>Upload a file:</h3>
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" id="submit" value="Upload" name="submit">
  </form>
  <!-- Response Output -->
  <div class="info" style="display:none"></div>
</div>

<div id="updiv">
<form id="uploadFile" enctype="multipart/form-data">
<h3>Upload a file:</h3>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" id="submit" value="Upload" name="submit">
</form>

<!-- Response Output -->
<div class="info" style="display:none"></div>

</div>

<script>
$('#uploadFile').submit(function()
{
  event.preventDefault();
  $("#updiv").fadeOut();
  $.ajax(
  { 
    url: "upload.php",
    type: 'POST',
    data: new FormData(this),
    contentType: false,
    processData: false,
    success: function(data)
    {
      $('.info').html(data);    
      $('.info').show();    
      $("#updiv").fadeIn();
    }
  });

});
</script>
Hmmm
  • 1,774
  • 2
  • 13
  • 23
  • I will try it in the next ffew minutes ! Thanks a lot for now ! –  Dec 11 '14 at 13:45
  • I tried and it works ! Thank you ! I was so frustrated ! Thank you thank you thank you ! –  Dec 11 '14 at 15:11
0

Using jQuery .serialize() on a multipart form will not send through the file data.

See this answer for more details but essentially you can do

data: new FormData($(this)[0])

instead of

data: $(this).serialize()

Additionally add

processData: false

To the $.ajax object paremeter.

On the subject of testing, have you checked that the form submission and file upload first works correctly if you submit the form without jQuery involved?

Community
  • 1
  • 1
M1ke
  • 6,166
  • 4
  • 32
  • 50