Patrioticcow
Patrioticcow

Reputation: 27058

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

Upvotes: 1

Views: 163

Answers (3)

Vaibhav Naranje
Vaibhav Naranje

Reputation: 78

Use this-

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

Upvotes: 1

dqhendricks
dqhendricks

Reputation: 19251

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

Upvotes: 1

mellamokb
mellamokb

Reputation: 56779

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 },
    ...
});

Upvotes: 3

Related Questions