Unexpected token php in javascript
Being new to this, I'm trying to pass a variable from PHP to Javascript. Show In my php page, I use a test variable:
In my .js file:
I've tried several things, but it seems I always get Unexpected token < when I include " What am I doing wrong ? Thanks ! asked Mar 9, 2016 at 16:01
AlexBAlexB 131 silver badge8 bronze badges 4 In order to use PHP code in any file, the web server has to run that file through the PHP processor. This is configured to happen by default with You could configure your server to process Without knowing more about the structure of what you're trying to accomplish, it's difficult to advise a "best" approach. Options include (but may not be limited to):
answered Mar 9, 2016 at 16:09
DavidDavid 195k34 gold badges191 silver badges269 bronze badges 1 If you have that in a js file (like somefile.js) then PHP isn't going to parse that file by default. In the PHP file that links to that JS you can output a script tag and the var you want like:
And make sure your script is linked in after that code; answered Mar 9, 2016 at 16:06
MajicBobMajicBob 4772 silver badges8 bronze badges 1 .js files are not compiled by PHP. The easiest workaround is to put the Javascript in a
Mr Lister 44.5k15 gold badges107 silver badges146 bronze badges answered Mar 9, 2016 at 16:06
furtivefurtive 1,5992 gold badges12 silver badges15 bronze badges
Unexpected token error when using PHP with JavaScriptUncaught SyntaxError: Unexpected token < JavaScript Error Handling: Unexpected TokenUnexpected end of input expected expression, got ', ' expected expression, got ')' expected expression, got keyword 'else' Have a JavaScript Unexpected Token Error? Check Your Syntax// 1 var printError = function(error, explicit) { console.log(`[${explicit ? 'EXPLICIT' : 'INEXPLICIT'}] ${error.name}: ${error.message}`); } try { // Extra comma in Math.max var value = Math.max(1, 2,); console.log(value); } catch (e) { if (e instanceof SyntaxError) { printError(e, true); } else { printError(e, false); } } // CHROME Uncaught SyntaxError: Unexpected token ) // FIREFOX SyntaxError: expected expression, got ')' var printError = function(error, explicit) { console.log(`[${explicit ? 'EXPLICIT' : 'INEXPLICIT'}] ${error.name}: ${error.message}`); } try { var name = "Bob"; if (name === "Bob") { console.log(`Whew, it's just ${name}`); else { console.log("Imposter!"); } } catch (e) { if (e instanceof SyntaxError) { printError(e, true); } else { printError(e, false); } } // CHROME Uncaught SyntaxError: Unexpected token else // FIREFOX SyntaxError: expected expression, got keyword 'else' JavaScript SyntaxError – Unexpected tokenSyntaxError: expected expression, got "x" SyntaxError: expected property name, got "x" SyntaxError: expected target, got "x" SyntaxError: expected rest argument name, got "x" SyntaxError: expected closing parenthesis, got "x" SyntaxError: expected '=>' after argument list, got "x" SyntaxError 01234 SyntaxError: Unexpected token ';' SyntaxError: Unexpected tokenSyntaxError: expected expression, got "x" SyntaxError: expected property name, got "x" SyntaxError: expected target, got "x" SyntaxError: expected rest argument name, got "x" SyntaxError: expected closing parenthesis, got "x" SyntaxError: expected '=>' after argument list, got "x" for (let i = 0; i < 5,; ++i) { console.log(i); } // Uncaught SyntaxError: expected expression, got ';' for (let i = 0; i < 5; ++i) { console.log(i); } function round(n, upperBound, lowerBound){ if(n > upperBound) || (n < lowerBound){ throw 'Number ' + String(n) + ' is more than ' + String(upperBound) + ' or less than ' + String(lowerBound); }else if(n < ((upperBound + lowerBound)/2)){ return lowerBound; }else{ return upperBound; } } // SyntaxError: expected expression, got '||' function round(n, upperBound, lowerBound){ if((n > upperBound) || (n < lowerBound)){ throw 'Number ' + String(n) + ' is more than ' + String(upperBound) + ' or less than ' + String(lowerBound); }else if(n < ((upperBound + lowerBound)/2)){ return lowerBound; }else{ return upperBound; } } SyntaxError: expected expression, got \"x\"\nSyntaxError: expected property name, got \"x\"\nSyntaxError: expected target, got \"x\"\nSyntaxError: expected rest argument name, got \"x\"\nSyntaxError: expected closing parenthesis, got \"x\"\nSyntaxError: expected '=>' after argument list, got \"x\"\n for (let i = 0; i < 5,; ++i) {\n console.log(i);\n}\n// Uncaught SyntaxError: expected expression, got ';'\n for (let i = 0; i < 5; ++i) {\n console.log(i);\n}\n function round(n, upperBound, lowerBound){\n if(n > upperBound) || (n < lowerBound){\n throw 'Number ' + String(n) + ' is more than ' + String(upperBound) + ' or less than ' + String(lowerBound);\n }else if(n < ((upperBound + lowerBound)/2)){\n return lowerBound;\n }else{\n return upperBound;\n }\n} // SyntaxError: expected expression, got '||'\n function round(n, upperBound, lowerBound){\n if((n > upperBound) || (n < lowerBound)){\n throw 'Number ' + String(n) + ' is more than ' + String(upperBound) + ' or less than ' + String(lowerBound);\n }else if(n < ((upperBound + lowerBound)/2)){\n return lowerBound;\n }else{\n return upperBound;\n }\n}\n Uncaught SyntaxError: Unexpected token// including jquery.js // this script is myapp.js // Get the json data instead of making a request $(".mybtn").on("click",function(e){ // Stop the default behaviour. e.preventDefault(); // var jsonUrl = this.href + "&json=1"; $.ajax({ url: jsonUrl, type: "json", method: "get", success: function(data){ // do something with the data alert(data); } }); }); ... ^------------------- does not exist.
|