-1

I'm trying to use a php code snippet for jQuery Cycle Slideshow, but the console gives me this error:

"Uncaught SyntaxError: Unexpected token < "

var startingSlide = <?php echo $_GET["thumb"] ?>;

$(slideshowContainer).cycle ({
    startingSlide: startingSlide
});

How can I use the PHP code correctly?

I'd appreciate you help.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Jaeeun Lee
  • 3,056
  • 11
  • 40
  • 60

1 Answers1

6

PHP doesn't parse files with a .js ending, but you could have the PHP parser parse javascript files, but that's a really bad idea, instead insert the PHP in the PHP file where it belongs, and get the data with javascript

<div id="some_element" data-startingslide="<?php echo $_GET["thumb"] ?>"></div>

then do

var startingSlide = $('#some_element').data('startingslide')

$(slideshowContainer).cycle ({
    startingSlide: startingSlide
});
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Why does it belong there and not just echoed straight to a variable? – bobkingof12vs Mar 18 '14 at 22:21
  • @bobkingof12vs - Isn't it explained in the answer? The title of the question says "javascript file", and javascript files are not parsed by PHP. Of course you could echo the value to a global, but it would still be in the PHP file, where PHP code belongs, not in a javascript file, and using a data attribute seems more approriate than a global. – adeneo Mar 18 '14 at 22:24
  • Ah, see I thought you meant echo the php where the variable belongs, seeing that you echoed it to a div as you did and not to a global as the op did. I guess more my question is why did you echo to the div and not the global? – bobkingof12vs Mar 18 '14 at 22:29
  • Cause globals are bad ! – adeneo Mar 18 '14 at 22:41
  • But, you took it to a global variable still, you just made a stop through the div... Why did you make a stop through the div? – bobkingof12vs Mar 19 '14 at 13:57
  • Because the OP is using an external JS file, and that file is not parsed by PHP, the PHP file where the DIV is on the other hand is parsed by PHP, so just echoing out the PHP data in a data attribute and getting it in the external file is easier, and wether or not startingSlide is global or not depends on where it's used. – adeneo Mar 19 '14 at 14:04