简体   繁体   中英

including php within a jquery append

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM