I have a Javascript variable screenName for twitter users. When they connect to my site, I want a php script to write their name in a txt file. How could I do this. I looked into writing files with AJAX but no luck.
<script type="text/javascript">
twttr.anywhere(function (T) {
var currentUser,
screenName,
profileImage,
profileImageTag;
if (T.isConnected()) {
currentUser = T.currentUser;
screenName = currentUser.data('screen_name');
$('#twitter-connect-placeholder').append("<p style='color:white;text-shadow:none;'>Howdy " +" " + screenName + "!");
var screenName = "<?= $js ?>";
}
});
</script>
<?php
$check = $_GET['track'];
if ($check == '1') {
$fp = fopen('data.txt', 'a');
fwrite($fp, "{$js}");
fclose($fp);
}
?>
If you view the source screenName equals “”
Any ideas on this?
In order to approach the problem, you’ll need to understand that the PHP is executed on the server side, before the page is sent to the user, and the Javascript is executed on the client side, in the user’s browser, i.e. after the PHP code. Obviously, you need the events to happen in the reverse order. An example implementation follows:
Using jQuery, you can do:
And, in your
writename.php: