Samson Akanbi Samson Akanbi - 1 year ago 58
PHP Question

Simple PHP script to update an HTML count variable

I am trying to update a count variable by refreshing a PHP script just for test. But as simple as it looks, it doesn't work. What can I be doing wrong. This is my HTML file called cron.html.

<!DOCTYPE html>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>This is just a cron example</title>
<!-- CSS and JS would go here -->

<h1>This is test <span id="count">0</span></h1>
<!-- JS might sometimes go here -->


And this is my cron_handler.php


$doc = new DOMDocument();
$file = "/home2/odforgng/public_html/cron.html";

if ($doc->loadHTMLFile($file)) {
$span = $doc->getElementsByTagName('span')->item(0);
$count = $span->textContent;

$doc->getElementsByTagName('span')->item(0)->nodeValue = $count;

echo 'file updated successfully';
else {
return false;

After refreshing cron_handler.php it should update cron.html to say This is test 1.

Answer Source

You count doesn't get updated because you never save it in a way that will survive the next time you run the script. Write the current count to an external file or database.

$doc->getElementsByTagName('span')->item(0)->nodeValue = $count;

//add this

See the manual

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download