简体   繁体   English

使用ajax发送数据,但在PHP中为空$ _POST

[英]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. 我试图将div的data属性保存在变量PHP中。 So I used $.ajax to send data with POST but it return an empty array. 所以我用$ .ajax通过POST发送数据,但是它返回一个空数组。 However, the POST is visible in the console with good data. 但是,POST可以在控制台中以良好的数据显示。

AJAX 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 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. 我一定做错了什么,但我真的不知道...感谢您的帮助。

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 由于您在AJAX调用中传递的POST变量的名称为thumbnail_id而不仅仅是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. 检查控制台中是否打印任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM