0

I have read append php in jquery and it appears that JSON may be the solution but I'm unsure how to combine the 2 blocks of code. Any assistance at all appreciated.

Basically if I have the following JQuery:

$( "li.share-google-plus-1 a span" ).append( '<span class="share-count">x</span>' );

This is tested at http://jsfiddle.net/HSCzf/1/

However in place of 'x' I need to include PHP to fetch the content... something like:

<?php 
$obj=new shareCount(get_permalink( $post->ID ));  
echo $obj->get_plusones();
?>

Can anyone assist me insert this PHP where the 'x' is?

Community
  • 1
  • 1
Paul Brown
  • 83
  • 11
  • try this: var data= "get_plusones(); ?>"; and '+data+' – AppleBud May 24 '14 at 13:08
  • why do you want to use apppend? – Poorya Mohammadi May 24 '14 at 13:13
  • @iCode4U it relates to a Wordpress plugin and I just need to inject this code where specified. It seemed the most appropriate method from my quite basic knowledge. Looking at the plugin it's not just a case of editing the file to achieve it. That is beyond me but may have been my first choice. – Paul Brown May 24 '14 at 13:51
  • I just want to thank everyone but I can't get anything to work. I can create the element as per the fiddle and can get static content in of course but getting the output of PHP script is proving tricky. It's Wordpress related so maybe that's causing issues. Not being a developer and havin spent most of today playing I'm going to admit defeat. I have no doubt all solutions suggested are fine. – Paul Brown May 25 '14 at 18:33

1 Answers1

1

You can do this in several ways:

If you have the jquery and php together in 1 file:

<?php
$obj=new shareCount(get_permalink( $post->ID ));  
?>

$( "li.share-google-plus-1 a span" ).append( '<span class="share-count"><?php echo $obj->get_plusones(); ?></span>' );

If your jquery is in a seperate js file, you can choose to add the url in the <head>:

<head>
  <script>
    var plusone_link = '<?php echo $obj->get_plusones();  ?>';
  </script>
</head>
Stefan
  • 1,248
  • 6
  • 23
  • I just want to thank everyone but I can't get anything to work. I can create the element as per the fiddle and can get static content in of course but getting the output of PHP script is proving tricky. It's Wordpress related so maybe that's causing issues. Not being a developer and havin spent most of today playing I'm going to admit defeat. I have no doubt all solutions suggested are fine. – Paul Brown May 25 '14 at 18:34