0

Diafol, anybody pls help.

i have this javascript code

<script>

            FB.init({
              appId  : '00000000000000',
              frictionlessRequests: true,
            });

          function sendRequestToRecipients() {
            var user_ids = document.getElementsByName("user_ids")[0].value;
            FB.ui({method: 'apprequests',
              message: 'Great App',
              to: user_ids, 
            }, requestCallback);
          }

          function sendRequestViaMultiFriendSelector() {
            FB.ui({method: 'apprequests',
              message: 'My Great Request'
            }, requestCallback);
          }

          function requestCallback(response) {
            console.log(response);
            for (var i = 0; i < response.to.length; ++i)
            {
            alert(response.to[i]);
            }
      }
      </script>

Now all i want to do is get the value that is in this

alert(response.to[i]);

and pass it to php and insert it into mysql for future use. Am very bad at javascript, pls help.

4
Contributors
7
Replies
70
Views
3 Years
Discussion Span
Last Post by accra
0

Well I'm not great at PHP but here's a start...

To pass a value client side to server side, you have two common options...

Send the value to a PHP script using Ajax or save the value in a input element of type hidden. This hidden element's value can be capture duiring the post event when a user clicks on a submit button for example.

0

thanks for you time, any examples to point to will be a grate start. thx

0

Using javaScript Ajax is fairly easy but if you don't have experience with javascript, you could use jQuery's post method which is really javascript Ajax on the backend but a lot less code.

To use jQuery, you have to include the source of the library in your head section.

Then in your javascript, use the post method where it logically makes sense.

 $.post( "test.php", { param: response.to[i]} );

Your PHP page needs to accept this posted data and process it, save to the DB, etc...

It's very challenging to give you a better example based on the info you provided.

I think it would be better for you to go back and read up on posting data to a PHP page using javascript/jQuery if what you are looking for is sending data back to the server without having to reload the page.

jQuery documentation: http://api.jquery.com/jquery.post/

0

Try this, change the #submit to match your button's name or id and also the post URL :

<script type="text/javascript">
      $(document).ready(function(){
        $("#submit").click(function(){
          $.ajax({
        type: "POST",
        async: false,//here you are synchrone
        url: "", //Put your URL here
        success: function(data) {
            //echo data from server side
          });
        });
      });
</script>

Buzz back if it's working or if there's error, do specify the error...

0

thank you imchivaa for your help. but is there a way i can nest the ajax script you posted with my script above?

i want to know if i can nest it like this

function requestCallback(response) {
            console.log(response);
            for (var i = 0; i < response.to.length; ++i)
            {
            //alert(response.to[i]);
             $.ajax({
        type: "POST",
            data: {
       zxy: response.to[i];}
}).done(function( msg )
      {
       alert( "Data Saved: " + msg );// see alert is come or not
     });
            }
      }});
0

i finally got it. Here is my solution

` <div id="facebook_invite"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>

      <script>

        FB.init({
          appId  : 'xxxxxxxxxxxx',
          frictionlessRequests: true,
        });

      function sendRequestToRecipients() {
        var user_ids = document.getElementsByName("user_ids")[0].value;
        FB.ui({method: 'apprequests',
          message: 'Great App',
          to: user_ids, 
        }, requestCallback);
      }

      function sendRequestViaMultiFriendSelector() {
        FB.ui({method: 'apprequests',
          message: 'xxxxxxxx Request'
        }, requestCallback);
      }

    function requestCallback(response){
        console.log(response);
        for (var i = 0; i < response.to.length; ++i)
        { 
     var lapaz = response.to[i];

        $.ajax({
          type: "POST",
          url: "fb.php",
          data: "user_ids="+ lapaz,
          success:function(data){
                             location.reload(); 
                          }

        });
        }
      }

</script>`

Edited by accra: error in code

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.