简体   繁体   English

我如何在php中添加此脚本

[英]How can i add this script in php

am trying to find out how to add this script to my php file , as i try echo it ,its not working. 我试图找出如何将此脚本添加到我的php文件中,因为我尝试回显它,因此无法正常工作。

am sure there is a way to add this, its my first time adding these scripts to php file ,maybe il learn something today and remmeber it thanks. 肯定有一种方法可以添加它,这是我第一次将这些脚本添加到php文件中,也许今天我会学到一些东西并致以谢意。

var counter = 1;
var auto_refresh = setInterval(
function () {
    var newcontent= 'Refresh nr:'+counter;
    $('#divID').html(newcontent);
    counter++;
}, 5000);

and the html div this is not needed to add to php as i have own div in php file 和html div这不需要添加到php中,因为我在php文件中有自己的div

<div id="divID"></div>

Close the php tags, open a script tag, paste your script there, close your script tag, re-open your php tag. 关闭php标签,打开脚本标签,将脚本粘贴到此处,关闭脚本标签,然后重新打开php标签。 ;) ;)

I recommend putting your js at the end of the file tho. 我建议将您的js放在文件末尾。

<?php
    // your php here
?>

<div id="divID"></div>
<?php
    // some php if you want to
?>

<script type="text/javascript">
  //Your javascript here
  var counter = 1;
  var auto_refresh = setInterval(
  function () {
    var newcontent= 'Refresh nr:'+counter;
    $('#divID').html(newcontent);
    counter++;
  }, 5000);
</script>

Normally, you won't put javascript in your php file... But if there's no other solution: 通常,您不会将javascript放在php文件中...但是,如果没有其他解决方案:

<?php

echo"
<script type=\"text/javascript\">
    var counter = 1;
    var auto_refresh = setInterval(
    function () {
       var newcontent= 'Refresh nr:'+counter;
       $('#divID').html(newcontent);
       counter++;
}, 5000);        
</script>
";

In PHP pass js code into variable PHP js代码传递到变量中

<?php 
    $script = "<script type='text/javascript'>
    var counter = 1;
    var auto_refresh = setInterval(
    function () {
      var newcontent= 'Refresh nr:'+counter;
      $('#divID').html(newcontent);
      counter++;
    }, 5000);  </script>";
?>

In HTML where you want to show/add 在要显示/添加的HTML

<html>
<head>
   <?php echo  $script; ?>
</head>
</html>

You can put it like this or you can put <script> part in the head of your page or in js file and include in page. 您可以像这样放置它,也可以将<script>部分放在页面的开头或js文件中并包含在页面中。

<?php
// code
?>

<script>
$( document ).ready(function() {
   var counter = 1;
   var auto_refresh = setInterval(
   function () {
       var newcontent= 'Refresh nr:'+counter;   
       $('#divID').html(newcontent);
       counter++;
   }, 5000);
});
</script>
<div id="divID"></div>

Note: you also need jquery. 注意:您还需要jquery。 If you don't use one you can include this in your page also 如果您不使用它,也可以在页面中添加它

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>

I just tested it and it's working perfectly. 我刚刚对其进行了测试,并且效果很好。 Currently my counter is 目前我的柜台是

Refresh nr:51

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

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