简体   繁体   English

将PHP代码与jquery / javascript结合

[英]combine php code with jquery/javascript

I have php code and jQuery/javascript. 我有PHP代码和jQuery / javascript。 I want to make visitor counter using jQuery and php code. 我想使用jQuery和php代码制作访问者计数器。

Here is the php code : 这是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) ; 
} 
?>

and this is jQuery/javascript code : 这是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>

How can I combine the jQuery/javascript code into php code? 如何将jQuery / javascript代码合并为php代码? This is my first time for using jQuery, and I still don't understand to use jQuery. 这是我第一次使用jQuery,但我仍然不懂使用jQuery。 So I need your help. 所以我需要你的帮助。 :D Thank you. :D谢谢。

This is the result: 结果如下:

在此处输入图片说明

I want to add "50" into the jQuery "00000", so the result is "00050" 我想在jQuery“ 00000”中添加“ 50”,所以结果是“ 00050”

You simply need to echo the counter value in the span element. 您只需要在span元素中回显计数器值即可。 So assuming your PHP code is at the top of the file, it should look like this. 因此,假设您的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) ; 
} 
?>
<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"><?php echo $counter; ?></span>
  <script>
     $('#custom').addClass('counter-analog').counter({
       //direction: 'up',
       interval: '1',
       format: '99999'
       //stop: '2012'
    });
  </script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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