1

Is there a way to make var changes be a variable that you can put into this array below?

    $(".SUBMIT").on("click", function(){
        var name = $("#idvalue").val();
        var changes = "thischanges"; // this variable changes the key 
        $.post("ajax_process.php",{ changes: name}, function(data){
            alert(data);
        });
    });// END submit

$_POST['changes']; // will be what sends through, is there a way to make it the variable every time?  

This would be similar to key, value but making a key that changes with your variable.

Sol
  • 949
  • 1
  • 11
  • 23

1 Answers1

2

You will need to construct object in two steps and use bracket notation for variable property name:

$(".SUBMIT").on("click", function(){
    var name = $("#idvalue").val();
    var changes = "thischanges";

    var data = {}
    data[changes] = name; // set dynamic property name

    $.post("ajax_process.php", data, function(data){
        alert(data);
    });
});

With ES2015 standard, you can use computed property names:

$(".SUBMIT").on("click", function(){
    var name = $("#idvalue").val();
    var changes = "thischanges";

    $.post("ajax_process.php",{[changes]: name}, function(data){
        alert(data);
    });
});
dfsq
  • 191,768
  • 25
  • 236
  • 258