简体   繁体   中英

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

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

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 ); */
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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