0

I am making a ajax request on a dynamically added element. My purpose is to change the image that has just been uploaded and added dynamically on the page (the image has a change button). Issue I am facing here is that after the ajax request PHP returns that no file is sent .i.e form is submitted before file was loaded with browse.

I cannot add submit button as only one button should be present.

Screenshot for UI (event is tied up on change button)

enter image description here

which is dynamically added by clicking and uploading that + button on the right (same change event is fired and it is working fine for that event).

Code:

$('.img-holder').on("change",".upload_form_change",function(){
    /*var file = _("file_new").files[0];
    var formdata1 = new FormData();
    formdata1.append("file_new", file);
    var ajax = new createXMLHTTPObject();
    ajax.upload.addEventListener("progress", progressHandler, false);
    ajax.addEventListener("load", changeCompleteHandler, false);
    ajax.addEventListener("error", errorHandler, false);
    ajax.addEventListener("abort", abortHandler, false);
    ajax.open("POST", "file_upload_parser.php");
    ajax.send(formdata1);*/

    var currentEle = $(this);
    console.log('fired');
    $.ajax({
      url: "file_upload_parser.php",
      type: "POST",
      data: new FormData(),
      contentType: false,
      cache: false,
      processData: false,
      success: function(data){
        //currentEle.html(img);
        console.log('success');
      }
    }, 1000);

  });

Commented section is what I am using on + icon ajax upload.

PHP

<?php
$fileName = $_FILES["file1"]["name"]; // The file name
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file1"]["type"]; // The type of file it is
$fileSize = $_FILES["file1"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true
if (!$fileTmpLoc) { // if file not chosen
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
}
if(move_uploaded_file($fileTmpLoc, "test_uploads/$fileName")){
    echo "$fileName upload is complete";
} else {
    echo "move_uploaded_file function failed";
}
?>

output array in response when adding echo print_r($_FILES); at top

 Array
(
    [file] => Array
        (
            [name] => Penguins.jpg
            [type] => image/jpeg
            [tmp_name] => E:\xampp\tmp\php3C47.tmp
            [error] => 0
            [size] => 777835
        )

)

Error that is being returned in post

Notice: Undefined index: file1 in E:\xampp\htdocs\file_upload_parser.php on line 2

Notice: Undefined index: file1 in E:\xampp\htdocs\file_upload_parser.php on line 3

Notice: Undefined index: file1 in E:\xampp\htdocs\file_upload_parser.php on line 4

Notice: Undefined index: file1 in E:\xampp\htdocs\file_upload_parser.php on line 5

Notice: Undefined index: file1 in E:\xampp\htdocs\file_upload_parser.php on line 6
ERROR: Please browse for a file before clicking the upload button.

HTML that is being appended

<div style="margin-bottom:10px; position: relative;" class="col-lg-4 text-center item">
<form class="upload_form_change" style="position:absolute;position: absolute;z-index: 9999;width: 71px;left: 40%;top: 36%;opacity: 0;" enctype="multipart/form-data" method="post">
    <input type="file" class="change-upload" name="file_new" id="file_new" value="&#43;">
</form><img src="test_uploads/'+ imageFileName[0] + '" alt="" class="img-thumbnail small">&nbsp;
<button class="btn btn-small btn-change"><a href="">Change</a>

Ignore the CSS

How can I debug this ?

Shashi
  • 474
  • 6
  • 21

0 Answers0