{"id":13044,"date":"2024-03-15T01:49:44","date_gmt":"2024-03-15T01:49:44","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/"},"modified":"2025-08-05T10:28:04","modified_gmt":"2025-08-05T10:28:04","slug":"how-to-write-code-for-real-time-data-refreshing-in-php","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/","title":{"rendered":"PHP Real-Time Data Refresh Using Ajax"},"content":{"rendered":"<p>You can achieve real-time data refreshing in PHP using Ajax technology. Here is a simple example code:<\/p>\n<p>The file index.php:<\/p>\n<pre class=\"post-pre\"><code>&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;script src=<span class=\"hljs-string\">\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.5.1\/jquery.min.js\"<\/span>&gt;&lt;\/script&gt;\r\n    &lt;script&gt;\r\n        $(document).<span class=\"hljs-title function_ invoke__\">ready<\/span>(function(){\r\n            <span class=\"hljs-title function_ invoke__\">setInterval<\/span>(function(){\r\n                $.<span class=\"hljs-title function_ invoke__\">ajax<\/span>({\r\n                    <span class=\"hljs-attr\">url<\/span>: <span class=\"hljs-string\">'getData.php'<\/span>,\r\n                    <span class=\"hljs-attr\">success<\/span>: function(data){\r\n                        $(<span class=\"hljs-string\">'#result'<\/span>).<span class=\"hljs-title function_ invoke__\">html<\/span>(data);\r\n                    }\r\n                });\r\n            }, <span class=\"hljs-number\">1000<\/span>);\r\n        });\r\n    &lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;div id=<span class=\"hljs-string\">\"result\"<\/span>&gt;&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/code><\/pre>\n<p>Obtain data from getData.php.<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-meta\">&lt;?php<\/span>\r\n<span class=\"hljs-comment\">\/\/ \u5728\u8fd9\u91cc\u7f16\u5199\u83b7\u53d6\u6570\u636e\u7684\u4ee3\u7801\uff0c\u6bd4\u5982\u4ece\u6570\u636e\u5e93\u4e2d\u67e5\u8be2\u6570\u636e<\/span>\r\n<span class=\"hljs-variable\">$data<\/span> = <span class=\"hljs-string\">\"\u5b9e\u65f6\u5237\u65b0\u6570\u636e\uff1a\"<\/span> . <span class=\"hljs-title function_ invoke__\">date<\/span>(<span class=\"hljs-string\">'Y-m-d H:i:s'<\/span>);\r\n\r\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-variable\">$data<\/span>;\r\n<span class=\"hljs-meta\">?&gt;<\/span>\r\n<\/code><\/pre>\n<p>The code above utilizes the jQuery library in the index.php page to make Ajax requests. After the page is loaded, an Ajax request is sent to the getData.php page every 1 second using the setInterval function to retrieve data, which is then displayed in the <\/p>\n<div id=\"result\"><\/div>\n<p> on the page.<\/p>\n<p>In getData.php file, code can be written to retrieve data, such as querying data from a database, and then returning the retrieved data to the index.php page.<\/p>\n<p>Please be aware that the example above is just a simple example and may need to be modified and improved according to specific requirements in actual applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can achieve real-time data refreshing in PHP using Ajax technology. Here is a simple example code: The file index.php: &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script src=&#8221;https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.5.1\/jquery.min.js&#8221;&gt;&lt;\/script&gt; &lt;script&gt; $(document).ready(function(){ setInterval(function(){ $.ajax({ url: &#8216;getData.php&#8217;, success: function(data){ $(&#8216;#result&#8217;).html(data); } }); }, 1000); }); &lt;\/script&gt; &lt;\/head&gt; &lt;body&gt; &lt;div id=&#8221;result&#8221;&gt;&lt;\/div&gt; &lt;\/body&gt; &lt;\/html&gt; Obtain data from getData.php. &lt;?php \/\/ \u5728\u8fd9\u91cc\u7f16\u5199\u83b7\u53d6\u6570\u636e\u7684\u4ee3\u7801\uff0c\u6bd4\u5982\u4ece\u6570\u636e\u5e93\u4e2d\u67e5\u8be2\u6570\u636e $data [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[17253,17255,17254,17256,17252],"class_list":["post-13044","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-ajax-php-tutorial","tag-javascript-ajax","tag-live-data-updates","tag-php-dynamic-content","tag-php-real-time-data-refresh"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP Real-Time Data Refresh Using Ajax - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn how to implement real-time data refreshing in PHP with Ajax through step-by-step code examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP Real-Time Data Refresh Using Ajax\" \/>\n<meta property=\"og:description\" content=\"Learn how to implement real-time data refreshing in PHP with Ajax through step-by-step code examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T01:49:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-05T10:28:04+00:00\" \/>\n<meta name=\"author\" content=\"Noah Thompson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Noah Thompson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/\"},\"author\":{\"name\":\"Noah Thompson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a\"},\"headline\":\"PHP Real-Time Data Refresh Using Ajax\",\"datePublished\":\"2024-03-15T01:49:44+00:00\",\"dateModified\":\"2025-08-05T10:28:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/\"},\"wordCount\":137,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"Ajax PHP tutorial\",\"JavaScript Ajax\",\"live data updates\",\"PHP dynamic content\",\"PHP real-time data refresh\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/\",\"name\":\"PHP Real-Time Data Refresh Using Ajax - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T01:49:44+00:00\",\"dateModified\":\"2025-08-05T10:28:04+00:00\",\"description\":\"Learn how to implement real-time data refreshing in PHP with Ajax through step-by-step code examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP Real-Time Data Refresh Using Ajax\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a\",\"name\":\"Noah Thompson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g\",\"caption\":\"Noah Thompson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/noahthompson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"PHP Real-Time Data Refresh Using Ajax - Blog - Silicon Cloud","description":"Learn how to implement real-time data refreshing in PHP with Ajax through step-by-step code examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/","og_locale":"en_US","og_type":"article","og_title":"PHP Real-Time Data Refresh Using Ajax","og_description":"Learn how to implement real-time data refreshing in PHP with Ajax through step-by-step code examples.","og_url":"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T01:49:44+00:00","article_modified_time":"2025-08-05T10:28:04+00:00","author":"Noah Thompson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Noah Thompson","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/"},"author":{"name":"Noah Thompson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a"},"headline":"PHP Real-Time Data Refresh Using Ajax","datePublished":"2024-03-15T01:49:44+00:00","dateModified":"2025-08-05T10:28:04+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/"},"wordCount":137,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["Ajax PHP tutorial","JavaScript Ajax","live data updates","PHP dynamic content","PHP real-time data refresh"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/","url":"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/","name":"PHP Real-Time Data Refresh Using Ajax - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T01:49:44+00:00","dateModified":"2025-08-05T10:28:04+00:00","description":"Learn how to implement real-time data refreshing in PHP with Ajax through step-by-step code examples.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-to-write-code-for-real-time-data-refreshing-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"PHP Real-Time Data Refresh Using Ajax"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a","name":"Noah Thompson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g","caption":"Noah Thompson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/noahthompson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/13044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=13044"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/13044\/revisions"}],"predecessor-version":[{"id":156893,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/13044\/revisions\/156893"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=13044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=13044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=13044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}