在 JavaScript 中加载 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19760422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:42:14  来源:igfitidea点击:

Load PHP in javascript

javascriptphp

提问by Peter

I've got some PHP code where it says the time you signed up, and gives an approximation of how long ago it was. I've been looking into trying to refresh it with javascript, but I'm struggling to find exactly what I'm after, and as my knowledge of it isn't good I'm a bit stuck on getting it working now.

我有一些 PHP 代码,上面写着您注册的时间,并给出了大约多久以前的信息。我一直在研究尝试用 javascript 刷新它,但我正在努力找到我想要的东西,而且由于我对它的了解并不好,我现在有点坚持让它工作。

Basically, this is the code I currently have, with the function being the thing displaying the time since singing up

基本上,这是我目前拥有的代码,功能是显示自唱歌以来的时间

Date registered:  <?PHP echo date('jS F Y', $_SESSION['User_Date'])." at ".date('G:i:s', $_SESSION['User_Date']);
        echo " (".time_delta('now',date('F j Y G:i:s', $_SESSION['User_Date'])).")"; ?>

Which will output something like:
Date registered: 4th November 2013 at 0:57:31 (About 34 minutes ago)

它将输出如下内容:
注册日期:2013 年 11 月 4 日 0:57:31(大约 34 分钟前)

And I've got this for javascript, but I don't know how to make it just update the text and not redirect the page.

我有这个用于 javascript,但我不知道如何让它只更新文本而不是重定向页面。

 <script>
    setInterval(function() {
    window.location.href = "timeupdate.php";
    }, 1000);
 </script>

Would there be a simple way to do this, or is it worth avoiding it in terms of cpu usage?

是否有一种简单的方法可以做到这一点,或者是否值得在 CPU 使用方面避免它?

回答by hammus

As the comments have suggested AJAX is your answer.

由于评论表明 AJAX 是您的答案。

With jQuery an ajax callwould look something like this:

使用jQuery,ajax 调用看起来像这样:

$(document).ready(function() {
    setInterval(function(){
       $.ajax({
           url: "timeupdate.php",
           type: "GET",
           dataType: "html",
           success: function(html) {
           $("#date_registered_element").html(html);
        }
      });//end ajax call
    },1000);//end setInterval
});//end docReady 

This will output the text returned from timeupdate.php to a div with the id of "date_registered_element" -> change this to the name of the div to which you wish to output the updated text.

这会将 timeupdate.php 返回的文本输出到 ID 为“date_registered_element”的 div -> 将其更改为您希望将更新文本输出到的 div 的名称。

In order to pass a session variable to the Ajax function you need to have the server echo out the Session variable before the script runs (i.e. as the page is rendered) something like this: HTML:

为了将会话变量传递给 Ajax 函数,您需要让服务器在脚本运行之前(即在呈现页面时)回显会话变量,如下所示:HTML:

<div id="user_date" style="display: none;">
    <?php echo $_SESSION["User_Date"]; ?>
</div>

Please NOTE:You need to be very careful with this. Anyone who looks at your markup in a browsers View Source or Developer Tools will be able to see the SESSION Variable echod out. You should only do this if the data represents no security risk. In my opinion you would be better passing no GET variables in the Ajax call and just referencing the SESSION variable in the timeupdate.php script. The point of SESSION variables is to provide state-like functionality to HTTP so you should never need to send SESSION variables over a HTTP request.

请注意:您需要非常小心。任何在浏览器查看源代码或开发人员工具中查看您的标记的人都将能够看到 SESSION 变量的回显。仅当数据不代表安全风险时才应执行此操作。在我看来,您最好不要在 Ajax 调用中传递 GET 变量,而只在 timeupdate.php 脚本中引用 SESSION 变量。SESSION 变量的重点是为 HTTP 提供类似状态的功能,因此您永远不需要通过 HTTP 请求发送 SESSION 变量。

That said, if you wish to do it the way you currently have it set up then the code is below.

也就是说,如果您希望按照当前设置的方式进行操作,则代码如下。

Once you have echoed out the SESSION variable onto the page, you can then collect the value from the id with jQuery and pass it to the Ajax function:

一旦您将 SESSION 变量回显到页面上,您就可以使用 jQuery 从 id 中收集值并将其传递给 Ajax 函数:

Javascript/jQuery:

Javascript/jQuery:

var url = "timeupdate.php?time=" + $("#user_date").text();

//pass the url to the Ajax function in the ajax function like this:
$.ajax({
    url: url,
    //the rest of your ajax function here
});

if you add jQuery to your page <head></head>tag BEFOREyour existing script this should work:

如果您现有脚本之前将 jQuery 添加到您的页面<head></head>标签,这应该可以工作:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">