1

In my form.php

<form method="POST" enctype="multipart/form-data">
  <input type="file" name="file_upload[]" id="file_upload" style="display:none;" required multiple accept="images/*" onchange="preview_image()">
</form>

And there is a submit button below.

In my jquery part

$('#submit').on('click', function(e) {
  var files = $('#file_upload')[0].files;
  var form_data = new FormData();
  form_data.append("file_upload",files);

  $.ajax({
    url:"execute/Script.php?request=submit",
    type: "POST",
    data: form_data,
    cache: false,
    contentType: false,
    processData: false,
    success: function(dataResult){
      var dataResult = JSON.parse(dataResult);
    
      if(dataResult.statusCode == 1){
        alert("Success!");
      }
    }
  });
});

And in my script.php

if($_GET['request']=="submit"){
  $gallery_G = "";

  foreach($_FILES['file_upload']['error'] as $key => $error){

    if($error==UPLOAD_ERR_OK){
      $tmp_name = $_FILES['file_upload']['tmp_name'][$key];
      $PhotoName = $_FILES['file_upload']['name'][$key];
                    
      move_uploaded_file($tmp_name,"../img/properties/$name");
      $gallery_G = $gallery_G.';'.$PhotoName;
    }
  }

  $sql = mysqli_query($conn,"INSERT INTO property(photos) VALUES('$gallery_G')");

  if($sql) {
    echo json_encode(array("statusCode"=>1));
  }
  else{
    echo json_encode(array("statusCode"=>2));
  }
}

But it returns me this:

Notice: Undefined index: file_upload in G:\Xampp\htdocs\execute\Script.php on line 31

Warning: Invalid argument supplied for foreach() in G:\Xampp\htdocs\execute\Script.php on line 31
{"statusCode":2}

Is there any solution? I need to send those photos using 'append' method. And need to get those photos in 'Script.php' so that I can process or upload them from there.

6
  • Is the submit button outside the form and type="button" ? Commented Feb 20, 2022 at 14:01
  • @mplungjan <button class="btn-4 btn-round-3" id="submit">Submit</button> Commented Feb 20, 2022 at 14:03
  • @mplungjan and yes, it's outside of the form Commented Feb 20, 2022 at 14:03
  • @mplungjan and yes, it's outside of the form Commented Feb 20, 2022 at 14:04
  • 1
    Warning: You are wide open to SQL Injections and should use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input! Even when your queries are executed only by trusted users, you are still in risk of corrupting your data. Escaping is not enough! Commented Feb 20, 2022 at 14:37

1 Answer 1

1

Try passing the form element to FormData. No need to call the append method afterwards.

  var files = $('form')[0];
  var form_data = new FormData(files);

By using the append() method, you are putting a [object FileList] in the $_POST array under the file_upload key

If you just want the files from the form, you would need a loop:

  var files = $('#file_upload')[0].files;
  var form_data = new FormData();
    
  for (var i = 0; i < files.length; i++) {
    form_data.append("file_upload[]",files[i]);
  }  
Sign up to request clarification or add additional context in comments.

2 Comments

But I've other fields and multiple checkbox too.
updated answer to pull only the file input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.