jQuery array set both key and value and send array via AJAX

Grozav Alex Ioan

I want to send key value pairs to my php page, but I need to set the key and value dynamically from data tags.

Here's the code I have until now:

function send(){
    var attr = new Array();

    var key = $('#add').attr('data-key');
    var value = $('#add').attr('data-value');

    attr.push({ key : value });

    var data = {attributes: attr};

    $.ajax({
            url: "ajax.php",
            type: "POST",
            data: data,
            success: function(result) {
                alert(result)
            }
    });

}

This is not the actual code, it's just the basic functionality.

The problem is here:

attr.push({ key : value });

The 'key' is not taken as the variable I've set.

Can I please get some help? I'd really appreciate it. Thank you very much!

RoToRa

Update:

Since ECMAScript 6 (2015) it's possible to use computed object property names, which is supported by all current browsers and other JavaScript environments:

attr.push({ [key] : value });


Original Answer:

Use the bracket notation:

Instead of attr.push({ key : value }); use

var object = {}; 
object[key] = value;
attr.push(object);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP Ternary statement within Associative Array to set both key & value?

From Dev

How to send an JavaScript array to PHP via jQuery.ajax?

From Dev

How to send an JavaScript array to PHP via jQuery.ajax?

From Dev

Send associate array with jquery ajax

From Dev

jQuery AJAX - push additional key/value pair into a serialized $_POST array

From Dev

Send two arrays or array with variable via ajax

From Dev

How to send a PHP array via AJAX?

From Dev

jquery push the key and value into array

From Dev

Push key, value to array: jQuery

From Dev

send with ajax a value changing array and compare it

From Dev

send with ajax a value changing array and compare it

From Dev

Send multidimentional array from JQuery AJAX to PHP

From Dev

How to send array without "[]" using jQuery $.ajax?

From Dev

get key and value of array via underscore each

From Dev

Add a key and value into an array via function

From Dev

PHP: Passing the JSON of Key-Value Array to Javascript: via Ajax vs direct echoing out

From Dev

Unable to post Javascript array via jQuery ajax

From Dev

Sending multidimensional checkbox array via ajax in jQuery

From Dev

Transfer array from Php to jQuery via Ajax

From Dev

Passing multidimensional array via jQuery AJAX for MYSQL

From Dev

POST Array from checkbox via Jquery AJAX

From Dev

Send php array to jquery ajax and make a each loop from that array

From Dev

Check key's array with value's array and set it as value

From Dev

Find the value that is in both array

From Dev

PHP - Set array key only if the value is not null

From Dev

how to set colon between key value in an array

From Dev

How to send object that contains array via AJAX POST in Django

From Dev

On Button Click Send Javascript Array To Rails Controller Via Ajax Request

From Dev

On Button Click Send Javascript Array To Rails Controller Via Ajax Request

Related Related

  1. 1

    PHP Ternary statement within Associative Array to set both key & value?

  2. 2

    How to send an JavaScript array to PHP via jQuery.ajax?

  3. 3

    How to send an JavaScript array to PHP via jQuery.ajax?

  4. 4

    Send associate array with jquery ajax

  5. 5

    jQuery AJAX - push additional key/value pair into a serialized $_POST array

  6. 6

    Send two arrays or array with variable via ajax

  7. 7

    How to send a PHP array via AJAX?

  8. 8

    jquery push the key and value into array

  9. 9

    Push key, value to array: jQuery

  10. 10

    send with ajax a value changing array and compare it

  11. 11

    send with ajax a value changing array and compare it

  12. 12

    Send multidimentional array from JQuery AJAX to PHP

  13. 13

    How to send array without "[]" using jQuery $.ajax?

  14. 14

    get key and value of array via underscore each

  15. 15

    Add a key and value into an array via function

  16. 16

    PHP: Passing the JSON of Key-Value Array to Javascript: via Ajax vs direct echoing out

  17. 17

    Unable to post Javascript array via jQuery ajax

  18. 18

    Sending multidimensional checkbox array via ajax in jQuery

  19. 19

    Transfer array from Php to jQuery via Ajax

  20. 20

    Passing multidimensional array via jQuery AJAX for MYSQL

  21. 21

    POST Array from checkbox via Jquery AJAX

  22. 22

    Send php array to jquery ajax and make a each loop from that array

  23. 23

    Check key's array with value's array and set it as value

  24. 24

    Find the value that is in both array

  25. 25

    PHP - Set array key only if the value is not null

  26. 26

    how to set colon between key value in an array

  27. 27

    How to send object that contains array via AJAX POST in Django

  28. 28

    On Button Click Send Javascript Array To Rails Controller Via Ajax Request

  29. 29

    On Button Click Send Javascript Array To Rails Controller Via Ajax Request

HotTag

Archive