1

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

4
  • This method should work. Define doesn't seem to work. What happens? Can you post a snippet of what the code looks like when you view source in your browser after it has been generated? Commented Oct 4, 2011 at 18:18
  • As the javascript being written out with php? If not, _SERVER[PHP_SELF] is unavailable. Commented Oct 4, 2011 at 18:20
  • 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 Commented Oct 4, 2011 at 18:24
  • possible duplicate of sending information back and forth with AJAX Commented Oct 4, 2011 at 18:28

3 Answers 3

3

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 },
    ...
});
Sign up to request clarification or add additional context in comments.

8 Comments

Use fiddler (fiddler2.com/fiddler2) or something to track what data is flowing back and forth instead of just saying "doesn't work". What doesn't work? What's happening? What's not happening? Why? Go through the process step-by-step systematically and see how far the data is going before something fails.
if i enable the alert 'alert( "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 happens with or without the change I suggested in my post (or both)?
actually with your change i get this in the alert Data Saved: Array( [0]=>123123123 )
So the data is now being passed. That's what you wanted right?
|
1
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

Comments

1

Use this-

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.