Christian Ivanov
Christian Ivanov

Reputation: 165

jQuery ajax data two variables

I'm trying to send some data via ajax with jquery

var name = $(".name").attr("data-name");
var value = $(".value").attr("data-value");

$.ajax({
    url: 'panel.php',
    type: 'post',
    data: {name: value}
}).done(function(){
    alert("saved!");
});

So how can name and value be two variables. Now only value is a variable but what about name?

Cheers

Upvotes: 4

Views: 20255

Answers (2)

Lotus
Lotus

Reputation: 2656

try this:

var name = "data-name";
var value = "data-value";
var dataObj = {};

dataObj[name]=value;

$.ajax({
    url: 'panel.php',
    type: 'post',
    data: dataObj,
}).done(function(){
    alert("saved!");
});​

Upvotes: 14

Akhil
Akhil

Reputation: 7600

You need to wrap it to a DTO (Data Transfer object):

var obj = {};
obj.name = name;
obj.value = value;

//Convert to a DTO Object
var dto = { 'myData': obj };

Upvotes: 1

Related Questions