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

9
  • Try single quotes! ie. var a = '<?php echo $a ?>'; , refer to this post Commented May 17, 2015 at 2:20
  • @AdrianTeh Tried '<?php echo(plugin_dir_url( __FILE__ ).\"images/vpreview_center.png\"); ?>'; but it still got converted to string Commented May 17, 2015 at 2:25
  • '<?php echo(plugin_dir_url( __FILE__ ) . /images/vpreview_center.png/); ?>'; perhaps removing the double quotes, and reversed to forward slashes. Maybe this helps. Commented May 17, 2015 at 2:28
  • @AdrianTeh Sorry, still the same Commented May 17, 2015 at 2:43
  • PHP does not process JS files. Commented May 17, 2015 at 3:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.