1

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!

5
  • 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 Commented May 17, 2020 at 22:39
  • 1
    Why do you have contentType: false and processData: false? Setting those to false will be the cause of your problem Commented May 17, 2020 at 22:44
  • @phil .remove is a just a type button with no links. Commented May 18, 2020 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. Commented May 18, 2020 at 15:10
  • THANKS! file_get_contents() worked for me! Commented Jul 10, 2024 at 13:29

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

5 Comments

I wouldn't recommend getting OP to move to JSON. All you had to say was "remove the contentType and processData options"
json_decode(stripslashes($_POST['json'])) is completely unnecessary in this answer
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. For the sake of the answer i would say you are right, there is extra code on it.
There is absolutely nothing wrong with posting application/x-www-form-urlencoded data
Yes removing the content type and process data solved my problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.