簡體   English   中英

沒有刷新頁面的php / js刷新變量

[英]php/js refresh variable without refresh page

我的php / js代碼有問題。

我需要檢查文件的每1秒內容,因此在我要加載頁面之前,我將adress.txt內容設置為“ localhost”,然后轉到我的網站並顯示localhost,現在我要編輯文本內容文件為“ 192.168.0.1”,因此我想自動將div文本設置為新內容而無需刷新,其仍為localhost而不是192.168.0.1

我的實際js:

    $(document).ready(
        function() {
            setInterval(function() {
                var randomnumber = Math.floor(Math.random() * 100);
                $('#show').text("<?php echo file_get_contents("adress.txt");?>");
            }, 1000);
        });

如何在不刷新的情況下將div文本更新為新的txtfile內容?

對不起,英語不好:)

PHP是一種服務器端語言。 您的<?php echo file_get_contents("adress.txt");?>僅在加載(或刷新)頁面時執行。 然后,如果您想做任何事情而無需刷新,則需要使用javascript。 如果要不刷新就調用某些PHP代碼,則可以使用AJAX。 如果您只想獲取某些文件的內容,甚至不需要PHP。

嘗試這個:

$(document).ready(function()
{
    setInterval(function()
    {
        $.get('address.txt', function(data)
        {
            $('#show').text(data);
        });
    }, 1000);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM