<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
-
Do you want to save the contents of $UID to a file, or import the contents of a file to $UID....or something else that I'm not getting?ampersand– ampersand2011年05月01日 06:22:39 +00:00Commented May 1, 2011 at 6:22
-
I want to save the UID that is returned by the javascript in a value called $UID so I can later use: echo $uid and be able to put it directly on the INSERTlisovaccaro– lisovaccaro2011年05月01日 06:24:36 +00:00Commented May 1, 2011 at 6:24
-
You could use the FB PHP SDK: github.com/facebook/php-sdkAdam Holmes– Adam Holmes2011年05月01日 06:27:48 +00:00Commented May 1, 2011 at 6:27
-
1Do an AJAX call to send the value from Javascript to the server, where you can store it in the database.Marc B– Marc B2011年05月01日 06:28:48 +00:00Commented May 1, 2011 at 6:28
2 Answers 2
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.
4 Comments
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.