ConnerAiken
ConnerAiken

Reputation: 194

Inserting php into javascript snippet

From what I have read in the search function, the javascript absolutely needs . This script works fine when i sub in sample vaiables for disqus_identifier and disqus_title but when I try to use a php variable that has been stored in the page, it breaks!

<div id="disqus_thread"></div>
<script>
    var disqus_shortname = 'sample';

    var disqus_config = function () {
      this.page.remote_auth_s3 = '*********';
      this.page.api_key = '********';
}
</script>

<script type="text/javascript">
        /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
        var disqus_identifier = "/posts/"<?php var_dump($postid); ?>;
       var disqus_title = <?php var_dump($title); ?>;

       /* * * DON'T EDIT BELOW THIS LINE * * */
       (function() {
         var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
       })();
</script>

<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

Upvotes: 0

Views: 74

Answers (1)

Michael Angstadt
Michael Angstadt

Reputation: 890

var_dump() isn't what you want, as it "dumps information about a variable", instead of just echoing the contents. (Docs) You want to take the $postid and render the literal string contained within the variable.

use

<?php echo $postid ?>

or

<?=$postid?>

instead.

You also need to surround it with quotes if you want the string literal and not the int representation, so

var post_id = "<?=$post_id?>";

will work or

var post_id = "This <?=$post_id?> is great";

Upvotes: 3

Related Questions