javascript - 如何使用JSON和PHP保存数据?

标签 javascript php json

我需要以下脚本的帮助,我是 JSON 新手,需要将一些数据存储到 MYSQL 数据库。 我怎样才能做到这一点?或者,您是否可以协助编写脚本并详细解释如何使用 Facebook Javascript SDK 将用户登录信息存储到 MYSQL 数据库。存储他们的姓名和 ID

<?php

$sApplicationId = 'YOUR_APPLICATION_ID';
$sApplicationSecret = 'YOUR_APPLICATION_SECRET';
$iLimit = 99;

?>
<!DOCTYPE html>
<html lang="en" xmlns:fb="https://www.facebook.com/2008/fbml">
    <head>
        <meta charset="utf-8" />
        <title>Facebook API - Get friends list</title>
        <link href="css/main.css" rel="stylesheet" type="text/css" />
    </head>
    <body>

        <img src="facebook.png" class="facebook" alt="facebook" />

        <center>
            <h1>Authorization step:</h1>
            <div id="user-info"></div>
            <button id="fb-auth">Please login here</button>
        </center>

        <div id="result_friends"></div>
        <div id="fb-root"></div>

        <script>
        function sortMethod(a, b) {
            var x = a.name.toLowerCase();
            var y = b.name.toLowerCase();
            return ((x < y) ? -1 : ((x > y) ? 1 : 0));
        }

        window.fbAsyncInit = function() {
            FB.init({ appId: '<?= $sApplicationId ?>',
                status: true,
                cookie: true,
                xfbml: true,
                oauth: true
            });

            function updateButton(response) {
                var button = document.getElementById('fb-auth');

                if (response.authResponse) { // in case if we are logged in
                    var userInfo = document.getElementById('user-info');
                    FB.api('/me', function(response) {
                        userInfo.innerHTML = '<img src="https://graph.facebook.com/' + response.id + '/picture">' + response.name;
                        button.innerHTML = 'Logout';
                    });

                    // get friends
                    FB.api('/me/friends?limit=<?= $iLimit ?>', function(response) {
                        var result_holder = document.getElementById('result_friends');
                        var friend_data = response.data.sort(sortMethod);

                        var results = '';
                        for (var i = 0; i < friend_data.length; i++) {
                            results += '<div><img src="https://graph.facebook.com/' + friend_data[i].id + '/picture">' + friend_data[i].name + '</div>';
                        }

                        // and display them at our holder element
                        result_holder.innerHTML = '<h2>Result list of your friends:</h2>' + results;
                    });

                    button.onclick = function() {
                        FB.logout(function(response) {
                            window.location.reload();
                        });
                    };
                } else { // otherwise - dispay login button
                    button.onclick = function() {
                        FB.login(function(response) {
                            if (response.authResponse) {
                                window.location.reload();
                            }
                        }, {scope:'email'});
                    }
                }
            }

            // run once with current status and whenever the status changes
            FB.getLoginStatus(updateButton);
            FB.Event.subscribe('auth.statusChange', updateButton);
        };

        (function() {
            var e = document.createElement('script'); e.async = true;
            e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
            document.getElementById('fb-root').appendChild(e);
        }());
        </script>

</body>
</html>

最佳答案

将数据存储在数组中,然后使用 json_encode()。

看看here .

在 for 循环中:

for (var i = 0; i < friend_data.length; i++) {
    results += '<div><img src="https://graph.facebook.com/' + friend_data[i].id + '/picture">' + friend_data[i].name + '</div>';

    $.ajax({
      type: "POST",
      url: "some.php",
      data: { id : friend_data[i].id, name: friend_data[i].name }
    });
}

在 some.php 中:

$tmpArray   = array('id' => $_POST['id'], 'name' => $_POST['name']);
$output     = json_encode($tmpArray); // <-- save this in your db, if you want to store a JSON string, but it doesn't seem to be optimized. You'd better to save ID and name.

关于javascript - 如何使用JSON和PHP保存数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20753167/

相关文章:

javascript - react 语法错误: Inline Babel script: Unterminated JSX contents

javascript - 滚动到绑定(bind)到 observableArray 的列表中创建的 DOM 元素

php - while 循环中的 mysql_query

php - mysql从第一个表插入两个表id

c# - C#对象转json字符串,如何处理双引号

javascript - 如何从两个不同的源中提取 JSON 数据?

javascript - jQuery 移动 (1.1) : disable hiding of a fixed navbar

JavaScript FancyTree : is it possible to distinguish between the selection of a node and the action of expanding it.

php - Zend Framework 如何优化模型?

ruby-on-rails - Rails、JSON 和加载时间