Jump to content

Syntax Error when I try to use php from javascript


MrScabby

Recommended Posts

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

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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 by MrScabby
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by MrScabby
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.