简体   繁体   English

如何使用JavaScript和PHP存储变量?

[英]how to use JavaScript and and php to store a variable?

i have a success function that has some data stored inside it: 我有一个成功函数,其中存储了一些数据:

function(receiverUserIds) {
console.log("IDS : " + receiverUserIds.request_ids);
}

this will log: IDS :123123213, 4645646654, 7897987989, .... 这将记录:IDS:123123213,4645646654,7897987989,....

what i want to do is grab all those id's and store them in the database. 我想要做的就是获取所有这些ID,并将其存储在数据库中。

one way i was thinking to do it is by using ajax: 我想做的一种方法是使用Ajax:

    function(receiverUserIds) {
        console.log("IDS : " + receiverUserIds.request_ids);
        $.ajax({
            type: "POST",
            url: "<?php echo $_SERVER['PHP_SELF']; ?>",
            friends_invite: receiverUserIds.request_ids,
            success: function(msg){
                /* alert( "Data Saved: " + msg ); */
            }
        });
}

and on the same page: 并在同一页面上:

if(isset($_POST['friends_invite'])){
print_r($_POST['friends_invite']);
}

but it doesn't seem to work. 但它似乎不起作用。

something might be wrong with either the ajax or i don't know. ajax可能有问题,或者我不知道。 maybe you guys can suggest another way of doing this..?? 也许你们可以建议另一种方法。

any ideas? 有任何想法吗?

Thanks 谢谢

edit: if i enable the alert lert( "Data Saved: " + msg ); 编辑:如果我启用警报功能lert( "Data Saved: " + msg ); i get an alert so i know that the ajax is successful, but i don't see my $_POST being echoed out 我收到警报,所以我知道ajax成功,但是我看不到$_POST被回显

This method should work fine. 此方法应该可以正常工作。 First thing that sticks out is that you should be using the data option to pass the data. 首先要指出的是,您应该使用data选项来传递数据。 See the specs of the documentation for more info. 有关更多信息,请参见文档规格

$.ajax({
    ...
    data: { friends_invite: receiverUserIds.request_ids },
    ...
});
friends_invite: receiverUserIds.request_ids

I don't belive JQuery works this way. 我不相信JQuery以这种方式工作。 It would have to be changed to something like: 它必须更改为以下内容:

data: 'friends_invite=' + receiverUserIds.request_ids

Use this- 用这个-

       $.ajax({
            type: "POST",
            url: "<?php echo $_SERVER['PHP_SELF']; ?>",
            'data':{friends_invite: receiverUserIds.request_ids},
            success: function(msg){
                /* alert( "Data Saved: " + msg ); */
            }
        });

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

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