0

I'm trying to save a data attribute of a div on click into a variable PHP. So I used $.ajax to send data with POST but it return an empty array. However, the POST is visible in the console with good data.

AJAX

$('.get-thumbnail-id').click(function() {
    var thumbnail_id = $(this).data('id');
    $.ajax({
        type: "POST",
        url: "gallery.php/",
        data: {thumbnail_id: thumbnail_id}
    });
});

GALLERY.PHP

var_dump($_POST['thumbnail_id']);

I must have done something wrong but I really don't know what... any help is appreciated, thanks.

1 Answer 1

1

You should be doing

var_dump($_POST['thumbnail_id']);

Because the POST variable you are passing in the AJAX call has name thumbnail_id not just id

and check it with

$.ajax({
        type: "POST",
        url: "gallery.php/",
        data: {thumbnail_id: thumbnail_id},
        success : function(data){console.log(data);}
    });

Check whether is prints anything in the console.

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

8 Comments

Yes sorry, wrong copy and past but this is not working
See the updated answer. echo the variable in php instead of var_dump and then make AJAX call and then check console.
echo also return null. The console.log(data) seems to works, it return me the good id : 56string(2) "56"
If console.log(data) is working that means the data is going to the PHP file. And if you try to access the PHP file directly it won't print aything. I hope you understand that.
Sorry but I don't understand what you mean
|

Your Answer

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