0

I'm trying to post the "lang_id" var. to "get_lang.php" with jquery to get (json) data. But I can't access the data.

Now trying to do

  var r = $(this).attr('rel');
  var v = data.r;

But this doesn't work because of "r" is string IMO.

Also tried

data.window[r] // but...

"get_lang.php";

$lang_id = (int) ($_POST['lang_id']);

if($lang_id == 1)
{
     $lang['simple'] = 'aaa';
     $lang['array'] = 'bbb';
}

if($lang_id == 2)
{
     $lang['simple'] = 'ccc';
     $lang['array'] = 'ddd';
}

print json_encode($lang);

my.js;

$.post("get_lang.php", { "lang_id": 2}, function(data){

   $('.lang').each(function() {

     var r = $(this).attr('rel');
     var v = data.r;

     $(this).text(v);

    });

},"json");

thanks for help.

2 Answers 2

1

Try

var v = data[r];

The dot notation interprets r as a string and not as a variable.

Sign up to request clarification or add additional context in comments.

Comments

0

I think maybe you need to tell jQuery the right content type. Put this at the TOP of your get_lang.php file

header("Content-Type: application/json");

And see if jQuery likes it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.