4 Replies - 461 Views - Last Post: 23 December 2015 - 01:18 PM

#1 howardbc14   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 21-October 15

Syntax error when loading PHP variables to JavaScript

Posted 23 December 2015 - 11:47 AM

PHP code
<?php
var $stats=5;
?>



jQuery code
var $results=<?php echo json_encode($stats);?>;



I would like to import PHP variable values into jQuery file. I wrote the following code, but I keep getting the following error in my console:
expected expression, got '<'

I have tried to edit the httpd-conf file to enable interactions between PHP and JS as some suggestions online have stated, but I am still getting the same error.

Is This A Good Question/Topic? 0
  • +

Replies To: Syntax error when loading PHP variables to JavaScript

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Syntax error when loading PHP variables to JavaScript

Posted 23 December 2015 - 12:05 PM

The keyword var is not used in that context, you should remove it.

Is your jQuery output from a .php file, after the php variable is defined?

(If your variable really is the number 5 then you do not need to json-encode it.)

This post has been edited by andrewsw: 23 December 2015 - 12:04 PM

Was This Post Helpful? 0
  • +
  • -

#3 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: Syntax error when loading PHP variables to JavaScript

Posted 23 December 2015 - 12:15 PM

Quote

expected expression, got '<'

looks like your file isn’t run through the PHP interpreter.
Was This Post Helpful? 0
  • +
  • -

#4 howardbc14   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 21-October 15

Re: Syntax error when loading PHP variables to JavaScript

Posted 23 December 2015 - 01:05 PM

View Postandrewsw, on 23 December 2015 - 12:05 PM, said:

The keyword var is not used in that context, you should remove it.

Is your jQuery output from a .php file, after the PHP variable is defined?

(If your variable really is the number 5 then you do not need to json-encode it.)



So you're saying that my jQuery output must be used with a web page published in .php format instead of .html, correct?

It was formatted in HTML as opposed to PHP.

View PostDormilich, on 23 December 2015 - 12:15 PM, said:

Quote

expected expression, got '<'

looks like your file isn’t run through the PHP interpreter.


My webpage is written in HTML. I will re-format the page as a PHP file. Am I now on the right track?
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: Syntax error when loading PHP variables to JavaScript

Posted 23 December 2015 - 01:18 PM

Quote

Am I now on the right track?

there’s not enough information to say definitely, but it looks right.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1