Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
        });
    });
}
like image 817
user3109875 Avatar asked Dec 17 '13 16:12

user3109875


People also ask

How can I pass multiple data from AJAX in PHP?

send multiple data using ajax $. ajax({ url: "/pakainfo_api", type: "GET", data: {p1: "value1", p2: "value2"}, // multiple data we want to send success: function(data){ console. log(data); } }). done(function(){ console.

Can you use AJAX with PHP?

Start Using AJAX Today In our PHP tutorial, we will demonstrate how AJAX can update parts of a web page, without reloading the whole page. The server script will be written in PHP. If you want to learn more about AJAX, visit our AJAX tutorial.


1 Answers

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, i.e. the keynames in javascript are also the key names for the $_POST array

like image 82
adeneo Avatar answered Oct 18 '22 10:10

adeneo