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…
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…;-(
code:
front-end:
$.post('common.php',{lang:lang});
PHP:
$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…
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.
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
});
… how does JS know you want a literal “lang” instead of the value of the variable lang which is in it’s variable table?
{lang:lang}
“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.
Example:
var foo = "bar";
obj = {foo: "bar"}; // result: obj.foo = bar
obj[foo] = "bar"; // result: obj.bar = bar
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…
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.
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.
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.
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.
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”