how to display multiple line file contents in javascript

I have a page where onclick on a button displays the text content from the file in the text area on the page

javascript function

function displayOutput()
    {
    <?php   
        $logfile = "hello";  
        $fh = fopen($logfile, 'r');  
        $size = filesize($logfile);  
        if($size == 0)  
                {  
            echo "Log file is getting generated";  
                }  
        else{  
            $theData = fread($fh, $size);  
            $data = trim($theData);  
            }  
        fclose($fh);  
    ?>  
    var html = "<?php echo $data; ?>";  
    document.form.text1.value = html;  
}  

Using this function i am able to correctly display the file contents only if the file contains a single line.

I want to display multiple lines in the textarea. I tried removing $data = trim($theData) but then no output is seen. If i echo $theData i am able to see all the file contents but in a single line. I also tried using while(!feof(filename)) but even that didn't work.

How do i display all the contents of the file on a new line using javascript?

Answers


Try this:

<?php   
    $logfile = "hello";  
?>
function displayOutput()
{
    var html = <?php
        echo filesize($logfile)
            ? json_encode(file_get_contents($logfile))
            : '"Log file is getting generated"'; 
        ?>;
    document.form.text1.value = html;
}

edited to include the filesize=0 condition


Need Your Help

Checking if a x,y position is within a dynamicly generated Elipse

c# random position ellipse

I'm trying to calculate whether or not a specific X,Y position lies within an ellipse in C#.

Error R14 (Memory quota exceeded) Not visible in New Relic

python django heroku

Keep getting a Error R14 (Memory quota exceeded) on Heroku.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.