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

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

问题描述:

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, ....

what i want to do is grab all those id's and store them in the database.

one way i was thinking to do it is by using 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. maybe you guys can suggest another way of doing this..??

any ideas?

Thanks

edit: if i enable the alert 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

我有一个成功函数,其中包含一些数据: p>

   function(receiverUserIds){
console.log(“IDS:”+ receiverUserIds.request_ids); 
} 
  code>  pre> 
 
 

这将记录:IDS:123123213 ,4645646654,7897987989,.... p>

我想要做的就是获取所有这些ID并将它们存储在数据库中。 p>

单向 我想这样做是通过使用ajax: p>

  function(receiverUserIds){
 console.log(“IDS:”+ receiverUserIds.request_ids); 
 $。  ajax({
 type:“POST”,
 url:“&lt;?php echo $ _SERVER ['PHP_SELF'];?&gt;”,
 friends_invite:receiverUserIds.request_ids,
 success:function(msg)  {
 / * alert(“数据已保存:”+ msg); * / 
} 
}); 
} 
  code>  pre> 
 
 

以及相同 页: p>

 如果(isset($ _ POST [ 'friends_invite'])){
print_r($ _ POST [ 'friends_invite']); \ N} 
 代码 >  PRE> 
 
 

BU 它似乎不起作用。 p>

ajax或我不知道可能有问题。 也许你们可以提出另一种方法来做这件事。?? p>

任何想法? p>

谢谢 p>

编辑:如果我启用警报 lert(“数据保存:”+ msg); code>我收到警报,所以我知道ajax成功,但我没有看到我的 $ _POST code>正在被淘汰 p> div>

This method should work fine. First thing that sticks out is that you should be using the data option to pass the 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. 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 ); */
            }
        });