John Davidson

javascript - js failure to read small text file with php

0 comments
Message:


Trying to read a small text file in php into a js variable. But the alert shows nothing even though the contents of "tmp/data.txt" = 30086895


function sort_rating() {reset_colors(); read_reg; 
myArray = myArray.sort(function(a, b){ return b.rate - a.rate;}); fortify(); newcomers();}

function newcomers() {
// data = '<?php clearstatcache(); echo readfile("tmp/data.txt"); ?>'; alert(data);
data = '<?php clearstatcache(); echo file_get_contents("tmp/data.txt"); ?>'; alert(data);
}

How can I make this work, please?



My Answer:
the text file are not empty. Here is my code:

PHP code to read the text file:

php
$file = fopen("textfile.txt", "r") or die("Unable to open file!");
$content = fread($file, filesize("textfile.txt"));
fclose($file);
echo $content;
?>


JavaScript code to read the PHP output into a variable:

javascript
var fileContent;
$.get("readfile.php", function(data) {
fileContent = data;
alert(fileContent);
});


The alert shows nothing even though the text file contains some text. What could be the issue here?

Rate this post

4 of 5 based on 8768 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us