如何使用ajax将多个值发送到PHP

时间:2013-12-17 16:43:15

标签: javascript php jquery ajax facebook-javascript-sdk

我正在尝试收集Facebook用户信息,然后进行签名。我如何在ajax中包含多个值?

$.signuser = function () {
    FB.api('/me', function (response) {
        var str = "";
        alert(response.name);
        var fbfname = response.first_name;
        var fblname = response.last_name;
        var fblname = response.id;
        var fblink = response.link;
        var fbusername = response.username;
        var fblink = response.email;
        $.ajax({
            type: "POST",
            data: {
                data: fbfname,
                fblname
            },
            complete: function () {
                //$('#booksloadjif').css('display','none')
            },
            url: "fbpost.php"
        }).done(function (feedback) {
            $('#fg').html(feedback)
        });
    });
}

1 个答案:

答案 0 :(得分:8)

您可以将多个键/值对作为$ .ajax

中的对象传递给PHP
$.signuser = function () {
    FB.api('/me', function (response) {

        var data = {     // create object
            fbfname    : response.first_name,
            fblname    : response.last_name,
            fblname    : response.id,
            fblink     : response.link,
            fbusername : response.username,
            fblink     : response.email
        }

        $.ajax({
            type: "POST",
            data: data, // pass as data
            url: "fbpost.php"
        }).done(function (feedback) {
            $('#fg').html(feedback)
        }).always(function() {
            $('#booksloadjif').css('display','none')
        });
    });
}

您可以使用

在PHP中访问它们
$_POST['fbfname'] 
$_POST['fblname'] 

等,即javascript中的键名也是$ _POST数组的键名