Jonas
Jonas

Reputation: 3

how to post multiple values using AJAX?

i have this code:

function Save(whichOne){
    var name = $('#name').val();
    var surname = $('#surname').val();
    $.ajax({
        url: 'SaveEntry.php',
        type: 'post',
        data: { "callFunc1": whichOne},
        success: function(response) {
            alert(response);
        }
    });
}

I want to do something like this:

function Save(whichOne){
    var name = $('#name').val();
    var surname = $('#surname').val();
    $.ajax({
        url: 'SaveEntry.php',
        type: 'post',
        data: { "callFunc1": {whichOne, name, surname}},
        success: function(response) {
            alert(response);
        }
    });
}

But it does not work. The problem is in this line:

data: { "callFunc1": {whichOne, name, surname}},

How do i post multiple values?

EDIT: I am getting this error: Warning: missing argument 2 for func1()

and i have this code:

if (isset($_POST['callFunc1'])) {
    echo func1($_POST['callFunc1']);
}

Upvotes: 0

Views: 71

Answers (2)

potato
potato

Reputation: 471

Depends on what you want to pass. It's either

data: [whichOne, name, surname] - that would be an array

or it's an object

data: { "whichOne" : whichOne, "name" : name, "surname" : surname }

Upvotes: 0

Jay Blanchard
Jay Blanchard

Reputation: 34406

You need key value pairs unless you want to send JSON data:

data: { "callFunc1": whichOne, "name": name, "surname":surname}

Upvotes: 3

Related Questions