2

I need to read a text file on a server and display its content in a blog post on Blogger. The text file is a result of a simple download counter and contains a number. The problem is the Blogger does not support PHP codes in a post. My current solution is to use OBJECT tag to call PHP script that displays the text file content with ECHO. It works. But the result is displayed inside a small frame and I can't apply CSS style to it or align it properly with the existing text. Is there another way? I understand it can be done with AJAX call but my scripting knowledge is basic and I wouldn't know where to begin. Help would be appreciated.

To display the result in the blog I used this code:

<p>File test.zip downloaded
<object type="text/plain" 
data="http://example.com/statistics.php?dname=test" 
width="30" height="30"></object> times</p>

EDIT: I have tried to follow @Toni suggestion but it only leads to more questions. Looks like Ajax call is way beyond my current level of knowledge. Sorry and thank you again.

Here is what I'm currently trying. I have moved the text that goes with the counter inside PHP file so the script now returns a string like "file has been downloaded 8 times" instead of just number "8". Also instead of OBJECT tag I'm using IFRAME.

<iframe src="http://example.com/mystats.php?dname=test"
 frameborder="0" border="0" cells pacing="0" height="30"></iframe>

The iframe seems to be easier to style. If I can't figure out how to find which CSS is applied to a blog post and how to apply it to iframe, I can at the minimum mimic the style by using similar font.

2 Answers2

1

You can use javascript with your blogger web-site.

Using javascript on your web-page, you can invoke a GET request to your PHP code and get the data you want, to display it on your web-page.

Below, there are links, to help you with this task:

How to invoke GET request in vanilla JavaScript

Invoking GET with jQuery

Use JavaScript to alter text dynamically

Community
  • 1
  • 1
  • Thank you for providing all the info and links! I've been reading through them and it's a slow going as most of the stuff is unknown to me. There is one thing that bothers me. It says the call will only work if it's the same domain. In my case the scripts are on my server but the actual blog is not. The blog is hosted by Blogger. – Natalia Linn Sep 15 '15 at 09:12
  • @NataliaLinn it is possible to make cross-domain requests. Check this link for example: http://stackoverflow.com/questions/17874730/how-to-make-cross-domain-request –  Sep 15 '15 at 13:59
0

I made it work with JavaScript! Here is how. Server side PHP script reads and echoes a text file inside document.write().

<?php
    $varcontent = @file_get_contents('yourtextfile.txt');
    echo 'document.write("'.$varcontent.'")';
?>

The resulting string looks like this:

document.write("your text file content here")

Inside the Blogger post add the JavaScript code with the PHP script file as a source:

<script type="text/javascript" 
    src="http://example.com/yourfile.php">
</script>

Done! The content of your text file is displayed and styled with your current CSS.