Categories: javascript, php

Refresh php function in div without page reload?

1 answer

I've made a more simple ping statues system for my servers. Right now my PHP function does a simple fsockopen and show Green/Red echo if up/down.

This ping status is only shown when the site is loaded.

Question : Can I somehow refresh and execute the function again without the whole page reload? I mean to have live status of servers changing if it goes down, without reloading the whole page.

<?php  function pingstate($host, $port) {    if (!$socket = @fsockopen($host, $port, $errno, $errstr, 0.1)){     echo "<p class='pingRed'><i class='fa fa-circle'></i></p>";   } else {     echo "<p class='pingGreen'><i class='fa fa-circle'></i></p>";     fclose($socket);   } }  ?> 

Example of output I use on other page than the function. This part works great but I have to "reload" the page everytime I want new status of the ping.

$host = $row['host']; $port = 80;             echo "<tr>               <td>" .$row['eq']."</td>               <td>".$row['ChargePoint']."</td>               <td>".$row['ip_addres']."</td>               <td>".$row['host']."</td>               <td><i class='fa fa-dashboard'></i></td>               <td><a href='http://".$row['host']."/' target='_blank'><i class='fa fa-gear'></i></a></td>                             <td>";                             echo pingstate($host, $port);                             echo "</td></tr>";   

I was thinking of maybe JavaScript that reloads the table row? How can this be done, I googled but cannot find for reload functions..

Please help me with explanation, I really want to learn.

Thank you!

All answers to this question, which has the identifier 61223230

The best answer:

First things first: its real bad coding style to use PHP to echo html. You should avoid it if possible.

My structure would look like this:

create a file called server_check.php and use your checking code inside like this:

  if (!$socket = @fsockopen($_GET["HOST"], $_GET["PORT"], $errno, $errstr, 0.1)){     echo json_encode("offline");     die;   } else {     echo json_encode("online");     fclose($socket);     die;       }   echo json_encode("no ping possible"); 

Next step is to create a html file with the following content. I'm using jQuery for simplifying your problem. You may use vanilla javascript if you prefer that.

let server_list = {    "example.com": 111,    "example1.com": 111  };    function checkServer() {    for (const [key, value] of Object.entries(object)) {      $.ajax({        url: "server_check.php",        data: {          HOST: key,          PORT: value        }      }).done(function(data) {        $('#'+key).text(data);      });    }    }    setInterval(checkServer, 10000); //Checks server each 10 secs
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> Info about Server EXAMPLE.COM Server is  <p id="example.com">NOT CHECKED</p>    Server EXAMPLE1.COM Server is  <p id="example1.com">NOT CHECKED</p>

EDIT: Please be aware of several security issues and non existent error handling. Please read on several topics (GET/POST/HTTP requests) and ajax in general before using this in a productive environment. If you have further questions feel free to ask them right away.

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data