0

I need to init some js variable by passing php variable to <some javascript>.js file, in my php project under Eclipe Luna IDE (PDT)

When I'm trying to do:

if (typeof strPreviewImg == 'undefined') {
            strPreviewImg = <?php echo(plugin_dir_url( __FILE__ )."images/vpreview_center.png"); ?>;
}

I've got following errors from Eclipse:

Multiple markers at this line - Syntax error, insert ": Expression" to complete Expression - Syntax error on token "<", invalid Expression - Syntax error on tokens, delete these tokens

When I'm trying to use quotes:

if (typeof strPreviewImg == 'undefined') {
                strPreviewImg = "<?php echo(plugin_dir_url( __FILE__ ).\"images/vpreview_center.png\"); ?>";
    }

Whole php expression is passed as string to my HTML code:

<video poster="<?php echo(plugin_dir_url( __FILE__ ).'images/vpreview_center.png)'; ?>">

So, am I doing something wrong or there's some Eclipse PDT issue?

Thanks

Samuel
  • 3,631
  • 5
  • 37
  • 71
  • Try single quotes! ie. `var a = ''; `, refer to this [post](http://stackoverflow.com/a/29612988/4749156) – daxeh May 17 '15 at 02:20
  • @AdrianTeh Tried `'';` but it still got converted to string – Samuel May 17 '15 at 02:25
  • `'';` perhaps removing the double quotes, and reversed to forward slashes. Maybe this helps. – daxeh May 17 '15 at 02:28
  • @AdrianTeh Sorry, still the same – Samuel May 17 '15 at 02:43
  • PHP does not process JS files. – SLaks May 17 '15 at 03:03
  • Ahh, checked out docs, https://codex.wordpress.org/Function_Reference/plugins_url, the arguments are also swapped. I'm eyeing this on mobile, unfortunately will not be able to test, but just code review. – daxeh May 17 '15 at 03:03
  • @SLaks But AFAIK, PDT should have JS support – Samuel May 17 '15 at 03:21
  • @SLake PHP can build Javascript files or code, and it can process forms submitted via AJAX. however, as you say, it does not process Javascript because, among other reasons, PHP is server-side and Javascript is client-side. they can only pass data to each other through some other bridge. – aequalsb May 17 '15 at 03:57
  • Thank you all guys. Actually looks like it's me being stupid. I should know that exernal JS files are parsed by JS itself, which have no idea about anything related to PHP. The only So no wonder it couldn't recognize my PHP variable :). http://stackoverflow.com/q/2928827/524743 – Samuel May 17 '15 at 05:55

1 Answers1

2

i have deducted from your use of plugins_dir_url() that you are using WordPress.

in WordPress, after using wp_enqueue_script() to register a script handle, you can "localize" data to be accessible globally on the client side.

with WordPress/PHP:

<?php
$args = array(
    'var1'  =>  'value 1',
    'var2'  =>  'value 2'
);
wp_localize_script( 'your_handle', 'your_cdata', $args );
?>

results in cdata in your document's <head>

<head>
<script type='text/javascript'>
/* <;![CDATA[ */
var your_cdata = {"var1":"value 1","var2":"value 2"};
/* ]]> */
</script>
</head>

then your javascript can use:

alert(your_cdata.var1);
alert(your_cdata.var2);

OPTIONALLY you can use PHP to serve Javascript files by setting your src URL to a PHP file:

<script type="text/javascript" src="url_to_your_php_file.php" /></script>

or with $_GET variables

<script type="text/javascript" src="url_to_your_php_file.php?var1=value1" /></script>

then in "your_php_file.php":

<?php
header( 'content-type: text/javascript' );
// possible database query here using $_GET[$var1]
$value = 'some value';
?>
function name() {
    var example1 = '<?php echo '"' . $value . '"'; ?>';
    var example2 = '<?php echo '"some other data"'; ?>';
    alert( example1 + ' / ' + example2 );
}
<?php
// and even do further includes to additional files (php, js, etc)
@include 'local_path_to_some_other_file.js';
exit;
?>

LASTLY you can replicate the WordPress example yourself:

<head>
<script type='text/javascript'>
/* <![CDATA[ */
var your_cdata = {
    "var1":<?php echo '"value 1"'; ?>,
    "var2":<?php echo '"value 2"'; ?>
};
/* ]]> */
</script>

note the use of single quotes enclosing double quotes. this makes the double quotes part of the output.

aequalsb
  • 3,765
  • 1
  • 20
  • 21