Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 583
Christian Ivanov Avatar asked Dec 06 '22 13:12

Christian Ivanov


1 Answers

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!");
});​
like image 160
Lotus Avatar answered Dec 25 '22 09:12

Lotus