lisovaccaro
lisovaccaro

Reputation: 33956

Save Javascript result in PHP value?

<script>
         FB.api('/me', function(user) {
           if(user != null) {
              var name = document.getElementById('uid');
              uid.innerHTML = user.uid
           }
         });
</script>           

UID:  <div id="id"></div>

I'm fetching the user ID and getting it to show by placing it in the element with id="uid" with a script. I copy pasted this code from facebook as I don't understand much of anything, anyhow the problem is I need to retrieve the UID many times in the site. In one particular case I need to set it as a value inserted in MYSQL database.

<?php
mysql_select_db("cyberworlddb", $con);
mysql_query("INSERT IGNORE INTO Badges (UID, Website, Code)
VALUES ('UID','$urlhost[0]','UID$urlhost[0]')");
?>

Is there a way to save the UID in a PHP value like $UID so I can retrieve it easily and add it to the database? Else what would be the best way to do it?

Thanks

Upvotes: 3

Views: 2988

Answers (2)

Thomas W Tupper
Thomas W Tupper

Reputation: 635

PHP executes server side whereas the JS is executing client side. You therefore cannot use JS to set a PHP variable; although in your markup the script and the PHP are on the same 'page', the client (web browser) never sees the PHP.

I'm not entirely sure why you want to post FB user ID's into a MySQL database, but assuming you were able to get the FB UID, you should be able to use an XHR to post it to your PHP.

That is, first get the FB ID client side, then using an Ajax POST deliver it to a PHP DB accessor. You can make it easier on yourself by using jQuery to do your ajax.

Here is in simple terms how that would work.

FIRST, on the page you are loading, get the UID:

<script>
         var UID;

         FB.api('/me', function(user) {
           if(user != null) {
              var name = document.getElementById('uid');
              name.innerHTML = user.uid;
              UID = user.uid;

           }
         });
</script>

Second, POST it to a PHP DB accessor:

<script>
jQuery.ajax({
        url: 'path to your PHP file',
        cache: false,
        type: 'POST',
        data: {theUID: UID}
    });
</script>

In your receiving PHP file, you would get the the POST var and use it to insert:

<?php

$UID = "-1";
if (isset($_POST['theUID'])) {
  $UID = $_POST['theUID'];
}

mysql_select_db("cyberworlddb", $con);
mysql_query("INSERT IGNORE INTO Badges (UID)
VALUES ('$UID')");

?>

Please understand that this is very rough - this is just a general example to point you in a direction that will work.

Upvotes: 2

Jasdeep Singh
Jasdeep Singh

Reputation: 3326

Make an Ajax call with the UID to the server that points to a PHP script that can handle your Ajax call. Otherwise you can use HTML post/get to post the data to your script synchronously, you are on the right track by adding the value to an element. You can have this as a hidden field in a form and post it.

Upvotes: 0

Related Questions