简体   繁体   中英

How to send multiple values using ajax to PHP

I'm trying to collect a Facebook user info and then sign them up. How do i include more than one value in 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)
        });
    });
}

You can pass multiple key / value pairs to PHP as an object in $.ajax

$.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')
        });
    });
}

and you'd access them in PHP with

$_POST['fbfname'] 
$_POST['fblname'] 

etc, ie the keynames in javascript are also the key names for the $_POST array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM