tags:

views:

34

answers:

1

Hi, i have a problem with the jquery post.

my vars

var checki = '1';
var my_var = 't95';

this works fine

var data = { 'mo': 'usr', 'dt': 'update_uis', 'values[0][t95]': checki']}; 

but when when i insert the var, it will not work

var data = { 'mo': 'usr', 'dt': 'update_uis', 'values[0]['+my_var+']': checki']}; // dont work



$.ajax({    type:           'post',
            cache:          false,
            url:            'tsal.php',
            data:           data,
            dataType:    'json',
            success:     function (data)
            {
                // blub
            }
});  

My problem is that i dont get an error. Hope somebody can help me.

Thanks in advance! Peter

+1  A: 

You need to add the variable dynamically, like this:

var checki = '1';
var my_var = 't95';
var data = { 'mo': 'usr', 'dt': 'update_uis' };
data['values[0]['+my_var+']'] = checki;
$.ajax({    
  type:           'post',
  cache:          false,
  url:            'tsal.php',
  data:           data,
  dataType:    'json',
  success:     function (data) {
    // blub
  }
});
Nick Craver
Hi nick! Thank you very much ! (as always) :)
Peter
The reason you have to do this is because in JavaScript, unlike languages like Python, the bit on the left of the `:` in an object literal is not a general expression. It can only ever be a single quoted string, or a non-keyword unquoted string.
bobince