I am trying to pass variable from JS to PHP. I have this JS code that runs in my footer.php:

function printResponse(response) {
    if ( response.errorCode == 0 ) {
        var NumberOfCommentsPerStream = response.streamInfo.commentCount;
    } else {
        alert('Error :' + response.errorMessage);

var NoumberOfCommentsParams = {
    categoryID: "iphones_comments",
   streamID: '<?php the_ID(); ?>',


as you can see I have a variable there which counts my gigya comments: NumberOfCommentsPerStream

it works fine when I am using alert. it shows the number of comments. I would like to integrate it to wordpress so I can echo it and display it in wordpress tried to simply echo it out:

<?php echo $NumberOfCommentsPerStream; ?>

of course it is not working. just trying to demonstrate what I am trying to achive anyone has a clue?

  • If you just want to display it you could add a blank div with a unique id (eg <div id="number_of_comments_per_stream"></div>) to your template and then set this in your printResponse routine: $('#number_of_comments_per_stream').html('Number of comments: ' + NumberOfCommentsPerStream); – bonger Aug 11 '14 at 14:02
  • I am trying to implement it to my JS code but can't seem to understand how to do it. I am kind of new to JS.. anyway you can give me a hint where exactly I need to put it in my JS code? Thanks! – gil hamer Aug 12 '14 at 7:00
  • Just after (or before!) where you have the commented-out alert(NumberOfCommentsPerStream) – bonger Aug 12 '14 at 7:14
  • I tried that and it looks reasonable but it didn't work.. I even tried to just regular HTML string and it didn't print it out. I will try one more thing and get back to you. thanks – gil hamer Aug 12 '14 at 18:35
  • Test to see if it's finding the div, either through the debugger or doing an alert($('#number_of_comments_per_stream').length); which should give 1 – bonger Aug 12 '14 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.