admin管理员组文章数量:1325079
I have this piece of JS code:
<script type="text/javascript">
var counter = 0;
function myFunction() {
counter++;
document.getElementById('countervalue').innerHTML = counter;
}
</script>
Which plusses a number with 1 everytime a button is pressed.
The variable "counter" decides what number to start with when the page is loaded. This number is currently 0. But it is supposed to be a number from the database. So I made a database connection using PHP and I now have the database number in a PHP variable. However, how do I implement it on the JS script? I have tried this:
<script type="text/javascript">
var counter = $clicks;
function myFunction() {
counter++;
document.getElementById('countervalue').innerHTML = counter;
}
The variable $clicks is the number from my database that is supposed to be the starting number when clicking the button. I tried this as well, with no luck:
var counter = <?php $clicks ?>;
I have this piece of JS code:
<script type="text/javascript">
var counter = 0;
function myFunction() {
counter++;
document.getElementById('countervalue').innerHTML = counter;
}
</script>
Which plusses a number with 1 everytime a button is pressed.
The variable "counter" decides what number to start with when the page is loaded. This number is currently 0. But it is supposed to be a number from the database. So I made a database connection using PHP and I now have the database number in a PHP variable. However, how do I implement it on the JS script? I have tried this:
<script type="text/javascript">
var counter = $clicks;
function myFunction() {
counter++;
document.getElementById('countervalue').innerHTML = counter;
}
The variable $clicks is the number from my database that is supposed to be the starting number when clicking the button. I tried this as well, with no luck:
var counter = <?php $clicks ?>;
Share
asked Apr 26, 2013 at 12:56
PHPeterPHPeter
771 silver badge8 bronze badges
1
- var counter = <?php echo $clicks ?>; should do – Satya Commented Apr 26, 2013 at 12:58
5 Answers
Reset to default 8You're close. You're just forgetting to echo your variable's content:
var counter = <?php $clicks ?>;
should be:
var counter = <?php echo $clicks ?>;
or, if you have short tags enabled:
var counter = <?= $clicks ?>;
var counter = <?=$clicks?>;
Remember the echo
.
var counter = <?php echo $clicks; ?>;
function myFunction() {
counter++;
document.getElementById('countervalue').innerHTML = counter;
}
var counter = <?php echo $clicks; ?>;
Counter value should be assigned as
var counter = <?php echo $clicks ?>;
本文标签: php variable together with javascript functionStack Overflow
版权声明:本文标题:php variable together with javascript function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742144938a2422750.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论