开发者

how to display multiple line file contents in javascript

开发者 https://www.devze.com 2023-01-22 03:11 出处:网络
I have a page where onclick on a button displays the text content from the file in the text area on the page

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{  
            $theDat开发者_开发知识库a = 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消