MrScabby Posted July 10, 2015 Share Posted July 10, 2015 Hi all I know this is one of those obvious things that you just cannot see when its patently obvious .....but! Why does this cause a syntax error? 1 outputBOXelement.innerHTML = <?php echo $outp; ?> =============================^ SyntaxError: syntax error output via http://www.javascriptlint.com/online_lint.php Quote Link to comment https://forums.phpfreaks.com/topic/297245-syntax-error-when-i-try-to-use-php-from-javascript/ Share on other sites More sharing options...
fastsol Posted July 10, 2015 Share Posted July 10, 2015 You might need to put single/double quotes around the entire php tags. What is the output of $outp? A string or integer? If it's a string value, then it likely needs quotes. Quote Link to comment https://forums.phpfreaks.com/topic/297245-syntax-error-when-i-try-to-use-php-from-javascript/#findComment-1516014 Share on other sites More sharing options...
scootstah Posted July 10, 2015 Share Posted July 10, 2015 You can't use a JS lint if you're getting the Javascript from PHP. You'd have to use lint on whatever PHP outputs. In a normal script, the PHP would have output a string before the JS ever ran. Quote Link to comment https://forums.phpfreaks.com/topic/297245-syntax-error-when-i-try-to-use-php-from-javascript/#findComment-1516015 Share on other sites More sharing options...
MrScabby Posted July 10, 2015 Author Share Posted July 10, 2015 I'm just trying to access the data contained in $outp, this contains a single record/row from a SQL database built into a string. Everytime I try to use some php script it triggers the syntax error and fails but the main problems Im encountering is getting some information about what went wrong. take the above, lets say the php needs double quotes and because I missed them it fails. How do I get information to use as a clue to the fault? the methods I have used so far are the error console in firefox, if this pulls up an error you can click the link to see some of the output. so this is ok I also check the syntax (as in this case) with an online checker. but after that Im stuck, if none of the above lists a message or error it just doesn't work and u have no clue where to start Quote Link to comment https://forums.phpfreaks.com/topic/297245-syntax-error-when-i-try-to-use-php-from-javascript/#findComment-1516019 Share on other sites More sharing options...
fastsol Posted July 10, 2015 Share Posted July 10, 2015 I can't speak for many scripting languages besides js and php, but the methodology of how certain values are handled is usually similar amongst languages. If the value is a number of some kind then usually you can get away with not wrapping it in quotes. But a string value always needs quotes cause the language won't understand what it is unless it by chance happens to be a standard function or something of the language, which if that happens you're up a creek anyway cause really unexpected things will happen. It really comes down to experience and lots of failures and making it work. Once you fail enough and see the kinds of errors that are thrown in your failed attempts, you will start to remember the ways you fixed them and not code that way again (or at least not as much). Quote Link to comment https://forums.phpfreaks.com/topic/297245-syntax-error-when-i-try-to-use-php-from-javascript/#findComment-1516020 Share on other sites More sharing options...
MrScabby Posted July 10, 2015 Author Share Posted July 10, 2015 (edited) lol, worst possible reply but thanks anyway, lol Timestamp: 10/07/2015 03:03:13 Error: SyntaxError: expected expression, got '<' Line: 23, Column: 43 Source Code: outputBOXelement.value = <?php echo $outp; ?>; Edited July 10, 2015 by MrScabby Quote Link to comment https://forums.phpfreaks.com/topic/297245-syntax-error-when-i-try-to-use-php-from-javascript/#findComment-1516021 Share on other sites More sharing options...
Ch0cu3r Posted July 10, 2015 Share Posted July 10, 2015 Where is that PHP code placed? If its in an external javascript file then that PHP code will never be parsed. Typically the server is only configured to parse files with .php file extension as PHP . PHP is a server-side language, meaning it is ran when a file has been requested from the server. What the browser see is only the output from your PHP code. Web browsers will not know what PHP code is and so your browser is trying to run the PHP code as javascript code. This is why you are getting the syntax error. There is also no point in checking your javascript code that has PHP code in it with a linter, they are only expecting javascript code. They will not know how to handle PHP code. Quote Link to comment https://forums.phpfreaks.com/topic/297245-syntax-error-when-i-try-to-use-php-from-javascript/#findComment-1516030 Share on other sites More sharing options...
MrScabby Posted July 10, 2015 Author Share Posted July 10, 2015 (edited) Where is that PHP code placed? If its in an external javascript file then that PHP code will never be parsed. Typically the server is only configured to parse files with .php file extension as PHP . PHP is a server-side language, meaning it is ran when a file has been requested from the server. What the browser see is only the output from your PHP code. Web browsers will not know what PHP code is and so your browser is trying to run the PHP code as javascript code. This is why you are getting the syntax error. There is also no point in checking your javascript code that has PHP code in it with a linter, they are only expecting javascript code. They will not know how to handle PHP code. Thank you for giving me a concise answer - I understand now and that answers many a frustrating hour or three pulling my hair out because what little clues I had to go on had no logical path I could follow to solve, you saying that now explains why there is no trail to follow. I really should have that in mind by now but trying to get javascript, php and HTML to hold together and talk to each other is a test of character I think, Is there some coffee club I can join when I've passed my initiation ceremony? thanks Edited July 10, 2015 by MrScabby Quote Link to comment https://forums.phpfreaks.com/topic/297245-syntax-error-when-i-try-to-use-php-from-javascript/#findComment-1516076 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.