0

the log file will be in notepad format the values will be like this 11.23445646,56.3456578954 10.23445646,26.3456578954 16.23445646,-46.3456578954 I'm planning to get the data from server to website textbox, of first value which I marked as italic the values will change after few seconds the updated value will come first. I tried some PHP example but not getting it in the below text box the values I need to get.. for example: x=11.23445646, y=56.3456578954, pls guide me

Longtitude <input id="x" type="number" value = "" onkeyup="updateMarker('x')">

Latitude <input id="y" type="number"value = "" onkeyup="updateMarker('y')">
kguest
  • 3,804
  • 3
  • 29
  • 31
mahesh
  • 17
  • 1
  • 6

2 Answers2

0

PHP doesn't really do "live" page updates since normally when a web browser (or other user agent) loads a web page once it's done downloading the page then PHP is already finished and can't touch what's already on the client.

Best way to do this would probably be to use a JavaScript AJAX call to periodically load the updated values from a PHP script and then update the values on the page.

Or if it's a really small page (in byte size) you could just make it automatically reload the whole page (with updated values) if that is not a problem for you.

In any case every time the PHP script is called it would just open the file in read mode and only read the latest values from the beginning of the file and return those. See fread(). Or maybe file_get_contents() or file() would be easier and just read the first line.

AJAX is a bit larger topic and I don't currently have the time to explain the whole process of updating the page using JavaScript. Google is your friend.

Haprog
  • 793
  • 1
  • 9
  • 21
0

Updated Answer

You can do this now using Web Socketing. Here is a guide and hello-wrold example of a php websocket server: http://socketo.me/docs/hello-world

And to see how to implement client side javascript of websocket, you can see the bottom of the link put above, which shows you this snippet:

var conn = new WebSocket('ws://localhost:8080');
conn.onopen = function(e) {
    console.log("Connection established!");
};

conn.onmessage = function(e) {
    console.log(e.data);
};

Old

PHP does not support live connections generally in the way you expect, you have to simulate it via repeated AJAX request. How? For instance on each second, or each two seconds.

You first have to write an ajax in your HTML with jQuery library:

Sending a request each second:

 var url  = "url_to_you_file";
    var textarea_id = "#textarea";
 
setInterval(function(){

   $.ajax({
     url : "site.com/get-file-logs.php",
     type : "POST",
     success : function(data){
      $(".textarea").html(data);

     }

   });
}, 1000); 

Then in PHP file you would write this:

$file_path = "path_to_your_file";
$file_content = file_get_contents($file_path);
echo $file_content;

The above example gets the file content and sends it back to your browser. You may want to process it in a certain way; that then changes your approach a little bit. Because you must always stick to JSON format when you try to get data back from server to be manipulated by Javascript.

Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105
  • in this any changes i have to make in the code you have given in ajax. like file name..thanks man – mahesh May 04 '15 at 07:32
  • pls check it var url = "no.txt"; (txt file name) var textarea_id = "x"; (text box id is x in html page) setInterval(function(){ $.ajax({ url : "logs.php", (php file name) type : "POST", success : function(data){ $(".x").html(data); (("" in the above code . is required r not x denotes the textbox id) } }); }, 1000); @ Mostafa Talebi – mahesh May 04 '15 at 10:27
  • Another solution is to detect new lines only (in the loop) and push them to a websocket. See this https://stackoverflow.com/a/33815809/2282880 for more info. – Meloman May 03 '22 at 08:27