【发布时间】:2015-11-27 10:10:37
【问题描述】:
我有 php 代码和 jQuery/javascript。我想使用 jQuery 和 php 代码制作访客计数器。
这里是php代码:
<?php
$handle = fopen("counter.txt", "r");
if(!$handle){
echo "could not open the file" ;
} else {
$counter = (int ) fread($handle,20);
fclose ($handle);
$counter++;
//echo $counter;
$handle = fopen("counter.txt", "w" );
fwrite($handle,$counter) ;
fclose ($handle) ;
}
?>
这是 jQuery/javascript 代码:
<html>
<head>
<title>jQuery Hello World</title>
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="js/jquery.counter.js"></script>
<link href="js/jquery.counter-analog.css" rel="stylesheet">
</head>
<body>
<span id="custom"></span>
<script>
$('#custom').addClass('counter-analog').counter({
//direction: 'up',
interval: '1',
format: '99999'
//stop: '2012'
});
</script>
</body>
</html>
如何将 jQuery/javascript 代码合并到 php 代码中?这是我第一次使用jQuery,但我仍然不明白如何使用jQuery。所以我需要你的帮助。 :D 谢谢。
这是结果:
我想在jQuery“00000”中添加“50”,所以结果是“00050”
【问题讨论】:
-
混合它们的最佳方式是使用 AJAX。在 JQuery 中查找任何 AJAX 示例。 AJAX 在“.html”页面和“.php”之间发送数据,无需重新加载网页。
-
可以使用
ajaxapi.jquery.com/jquery.ajax来完成 -
在您的情况下,您似乎只需要在页面上的某处使用
<?PHP echo $counter; ?>,除非您希望计数器在每个用户访问时计数,例如当另一个用户同时访问时更新一个页面上的计数器 -
嗯...我有 在我的 php 代码中,但问题是,如何从 php 代码中获取值并设置到 jquery 代码中..
标签: javascript php jquery counter visitor-pattern