0

I have some code

<script>

            FB.Event.subscribe('auth.login', function(response) 
        {

            FB.api('/me', function(response) {
      alert(response.id);

    });
</script>

An alert screen appears and shows the value i want (response id). but i would like to assign this value to a php value called $id i know how to do this but because the value is in script it wont work, i have tried adding in the below code without success

</script>  <?php $id ?> <script> response.id </script> <?php ; ?> <script>

this code is added just below the alert(response.id);

2 Answers 2

2

If you want to use the value on the server side the only way is to send it with AJAX to the server and after that store it in the Database or do something.

You can do something like this

<script>

    FB.Event.subscribe('auth.login', function(response) 
    {

        FB.api('/me', function(response) {
           $.ajax({ type: "POST", url: "save-fb-id.php",data: {id: response.id} });
        }

    });
</script>

Forgot to mention this is jQuery implementation

In your PHP script do the following:

<?php
    $fbID = $_POST['id'];

    save_to_db($fbID);
?>
Sign up to request clarification or add additional context in comments.

5 Comments

i want to add the value to my database anyway, is there a way of doing this with script?
Disagree. Another way is to use links. e.g. you can create link with javascript (do_something_with_my_var.php?value=something_from_js) and send value to the server this way. Of course, AJAX will do it without reloading page but still there's another way. Generally, you're right for +1.
Yeah you are right there are always other ways to do it but that is the most practical way. Just from curiosity, what will happen if the user doesn't click on the link or maybe closes the page? :)
this code is after the user logs in via facebook. so its dependent on them signing in before this code is run, thanks very much for the help
sorry if i want to post more than just one value, i assume i can just use a comma and add the next value?
0

PHP code is executed by the server before it sends the page to the client. Anything in a script tag is executed by the client. In order to send a value back to PHP, you'll have to submit some kind of request back to the server - it could be a hyperlink (a href='page.php?id=whatever), a form with a hidden value, an ajax request, etc

To add the value to a database, you'll need a php page (eg mypage.php) to insert your entry, with something like

if (isset($_GET['insertme'])) {
  mysql_query("INSERT INTO myidtable (id) VALUES ('{$_GET['insertme']}')");
}

And in your script, find some kind of ajax function and send a request to mypage.php?insertme=myid, where mypage is your database inserting page and myid is the id you want to insert.

Note that it's unsafe to insert values from $_GET directly into your database like that - you'll want to filter out single quotes first to prevent sql injection.

1 Comment

i want to add my value to my database what would be the best way to do this?

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.