简体   繁体   中英

How to put value of span tag in php variable

I have <span id="count"></span> which is basically changing its value on click.

Part of my javascript code is

$( document ).ready(function() {

  $('#stars').on('starrr:change', function(e, value){
    $('#count').html(value);
  });      

});

How to store the value of id count in a variable in php.

I tried:

<?php $var = ?><span id="count"></span><?php ; ?>

But I am getting error.

I cant store it in hidden field because its value changing on click.

Is there a way such that When I click a "Post" button whatever the value of #count it get stored it in php variable so I can insert into database.

Thanks.

JavaScript is client side and PHP is server side

You must send your value to server with ajax

$(document).ready(function() {
    $('#stars').on('starrr:change', function(e, value) {
        $('#count').html(value);
        $.ajax({
            url: "saveVariable.php",
            data: {value:value}
        }).done(function() {

        });
    });

});

saveVariable.php

$var=$_REQUEST["value"];

Try like this...

$value = "<script type='text/javascript'> $('#count').html();</script>"; ?>
echo $value;

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