0

I've seen a lot of posts on this issue, but many of them are at least 3 years old, and nothing seemed to resolve my issue. I'm hoping someone can help. I created an array of strings that are paths to files on a server. I want to pass that array to a PHP file to unlink the specified files. The problem is when I pass the array to the PHP file using jQuery's ajax() function, the $_POST variable is empty so I can't do anything with the data. The URL is good and the array has a length of 1 in this example. Here is my code:

jQuery

$(".remove").click(function() {
    var images = document.getElementsByTagName('img');
    var images2remove = [];

    $(images).each(function() {
        if($(this).hasClass('highlight')) {
            var path = $(this).attr('src');
            images2remove.push(path);
        }

    })

        $.ajax({
        url: 'removeFiles.php',
        type: 'post',
        data: {images : images2remove},  
        contentType: false,
        processData: false,
        success: function(response) {
            if(response != 0) {
              console.log(response);
            }
            else {
                alert('file not removed');
            }
        }
    })
});

And my php

$images = $_POST['images'];
var_dump($images);  // returns array(0){}

I've also tried:

$post = file_get_contents('php://input'); 
var_dump($post) // returns string(15) [object, Object] 

I'm not quite sure what to do with that. Based on examples I have seen I feel I should be able to access the contents of this array using the $_POST variable. What am I missing here? Thanks as always!

Skins fan
  • 25
  • 1
  • 10
  • Where are you seeing that `$_POST` is empty? Is it from `console.log(response)` or somewhere else? Also, what is your `.remove` HTML element? You're not preventing the default event action so if it's a link or submit button, your page might be navigating away – Phil May 17 '20 at 22:39
  • 1
    Why do you have `contentType: false` and `processData: false`? Setting those to `false` will be the cause of your problem – Phil May 17 '20 at 22:44
  • @phil .remove is a just a type button with no links. – Skins fan May 18 '20 at 14:54
  • contentType: false was the problem. Simple fix. Thank you. I was copying code off of an article I saw and never really thought about it. – Skins fan May 18 '20 at 15:10

1 Answers1

2

Check out this fiddle example:

https://jsfiddle.net/f7kbL4m2/8/

I had to change the data parameter to json to comply with the async call.

In your PHP

$data = json_decode(stripslashes($_POST['json']));

  // here i would like use foreach:

  foreach($data as $d){
     echo $d;
  }

Also, check out this answer

Remove the content type and process data from your AJAX Call.

$.ajax({
        url: 'removeFiles.php',
        type: 'post',
        data: {images : images2remove},  
        success: function(response) {
            if(response != 0) {
              console.log(response);
            }
            else {
                alert('file not removed');
            }
        }
    })

Content type: Indicate the type of data you are sending

Process data: Indicate that jquery should serialize the data to send it to the server.

Check out : https://api.jquery.com/jquery.ajax/

Roldan
  • 225
  • 2
  • 14
  • I wouldn't recommend getting OP to move to JSON. All you had to say was _"remove the `contentType` and `processData` options"_ – Phil May 17 '20 at 22:41
  • `json_decode(stripslashes($_POST['json']))` is completely unnecessary in this answer – Phil May 17 '20 at 22:47
  • I believe that sending raw post data over it's not a good practice. There have been a lot of different position on this.[Here is other discussion](https://stackoverflow.com/questions/8604717/json-vs-form-post). For the sake of the answer i would say you are right, there is extra code on it. – Roldan May 17 '20 at 22:59
  • There is absolutely nothing wrong with posting `application/x-www-form-urlencoded` data – Phil May 17 '20 at 23:02
  • 1
    Yes removing the content type and process data solved my problem. – Skins fan May 18 '20 at 15:28