user4592302
user4592302

Reputation:

Send data with ajax but empty $_POST in php

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.

Upvotes: 0

Views: 658

Answers (1)

void
void

Reputation: 36703

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.

Upvotes: 1

Related Questions