AJAX issue


I have a site in which I switch languages; I accomplish this very easily with jQuery/local storage; no problems at all; however; I do have some content that is generated from the back-end, with PHP… so I thought I would pass this lang var (either “en” or “es”) to the back-end with AJAX… no sweat, right? but it’s not working as expected… when I send the var to the back-end with AJAX and then do an echo PHP stmt to print out the var it prints only on the console (‘net’ tab on firebug), not on the browser… the var I create in PHP that evaluates to what came in from AJAX either doesn’t print at all on the browser or it throws an error…:frowning:

now the AJAX url is actually a file-include ( include ('common.php'); ); it’s just a file where I have env & global vars… so I thought I would just send the this lang var via AJAX to this same common.php file so then I could grab it from anywhere… dang, it didn’t work…;-(





$langFmAjax = isset($_REQUEST['lang']) ? $_REQUEST['lang'] : NULL;
    echo "$langFmAjax<br>";  // prints only on console, not browser
    $language = $langFmAjax;
    echo "language -- $language<br>"; // prints only on console, not browser

if ($langFmAjax === "es") { 
    echo "ES<br>"; // prints only on console, not browser
    $testLang = "Espanol"; 
if ($langFmAjax === "en") { 
    echo "EN<br>";  // prints only on console, not browser
    $testLang = "English"; 

echo "testLang -- $testLang<br>"; // throws error - undefined variable

does this mean that when you send something (in this case just a string variable) to the back-end with AJAX you can’t use it on the back-end?? you can only use it by sending it back to the client? but I need it on the back-end, that’s why I’m sending it to the back-end with AJAX… :wink:

am I missing something?

thank you…

You didnt put quotes around the first “lang”, so… you’re not sending {“lang”:“en”} you’re sending {“en”:“en”}. It’s using the variable value.

echo in an PHP call puts the output into the return - if you dont specify what to DO with your return when it gets back, Javascript wont… do anything with it.

I suggest you re-read the jQuery API you’re invoking.

No, quotes isn’t required in that case. His JS is ok.

That’s right. You’re sending AJAX request in background, so everything server will return (with echo’s) also available in background. If you want to get server response you should set a callback:

$.post('common.php',{lang:lang}, function(response){
    alert(response); // response will contain all output made by server

Check more details on @StarLion’s link

… how does JS know you want a literal “lang” instead of the value of the variable lang which is in it’s variable table?


“Well, OBVIOUSLY what i want is a string literal and a variable value. Or… is it two string literals? or is it two variable values? variable value and then a string literal? Uh… Parser Guess?”

No, it thinks: “Well, this is a string literal and I don’t care about variables with the same name”.
You can’t pass property name from variable if you use curly braces notation.
If you want use variable value as a property name you should use array-like notation.


var foo = "bar";
obj = {foo: "bar"};  // result: obj.foo = bar
obj[foo] = "bar"; // result: obj.bar = bar

Interesting. I learned something today.

It’s still REALLY bad form to have {aname:aname} and have it be two different things. It SHOULD still be {“aname”:aname}, to meet specification.

Sure, JSON spec requires quotes.
But, if we don’t use quotes around foo here:

obj.foo = "bar";

I think it’s more essential to avoid them here too:

obj = {foo: "bar"};

Of course, this is just a personal preference.

I would be interested in the part of the ECMAScript 2015 specification that defines that syntax.

ECMAScript? I’m using the actual JSON specification. Because you’re defining a JSON document.

The key in the key/value pair must be a string, and a string must start and end with a quote.


part of an object that associates a key (either a String value or a Symbol value) and a value

Specification allows both cases, with and without quotes.

1 Like

does… that not mean that the specification says that the key SHOULD be evaluated? and thus use the variable table? Or am i misunderstanding a Symbol value…

regarding the code we talk about is

$.post('common.php',{lang:lang}, function(response){

then it concerns the data argument, which according to the jQuery docs is

Type: PlainObject or String

by which it falls under the ECMAScript specification.

JSON specification isn’t related to this question.
You can read in its introduction:

JSON was inspired by the object literals of JavaScript aka ECMAScript as defined in the ECMAScript
Language Specification, third Edition

Inspired. That means JSON object and JavaScript objects isn’t the same thing.

No: http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-language-types-symbol-type
Symbol Type assumes immutable value, that means you can’t use variables or operations like {"prop"+"name": "value"}.

I’m still not actually seeing a syntax specification. I see definitions of properties of objects, which is fine - If i say { wark : “simple” } and wark has a value of “spoon”, the object can still be created as an immutable string key - the result being the valid object { “spoon”:“simple” }. Variable declaration does not invalidate the specification you have pointed to, because the object created is not a reference to the variable used. The obvious example of this is “well you can create them using THIS syntax” for the same object. Since the specification defines the object, and not the syntax for CREATING the object, the specification does NOT preclude using variables in the object creation - or else obj[imathing] = var would be invalid. - so there’s another definition somewhere that says “You may not use variables here.”

that’s not correct. the specification (as linked to by @megazoid) explicitly defines strings and symbols to be allowed. and a variable is not a symbol …

And when the object is created, the key IS a string - the specification, again defines the object, not the means of creation of the object, or obj[imathing] is invalid.

Immutable value can’t have a result, it’s constant. So that logic cannot be applied here.

Creating objects: http://www.ecma-international.org/ecma-262/5.1/#sec-11.1.5

It is a list of zero or more pairs of property names and associated values, enclosed in curly braces. The values need not be literals; they are evaluated each time the object initialiser is evaluated.

Only values are evaluated. But not property names.
Ok, so what is property name? http://www.ecma-international.org/ecma-262/6.0/#sec-object-type

A property name is a property key

Hmm… so what is a property key?

A property key value is either an ECMAScript String value or a Symbol value.

String or Symbol. Not an Expression. So, I think specification clearly says you cannot use variables as property names when you create an object with curly braces.

1 Like

Interesting. I finally found the syntax. Apparently you absolutely -can- have a variable name (or an expression) there, but you have to put it in . Which… sounds an lot like an ambiguity distinguisher, except that the default is to ‘assume string’. Makes readability awful, though.

If you imagine this

obj = {a: "foo", b: "bar"};

as a shorter version of this:

obj.a = "foo";
obj.b = "bar";

there won’t be any problems with readability anymore

But as a coder, if i see

{ avariable : avariable }

I dont think “one of those is a string”. This is probably a flaw of my brain saying “javascript doesnt use variable identifier marks (like PHP’s $), so anything not in quotes is a variable name”