Page 1 of 1

Text file to Textarea

Posted: Fri Sep 12, 2008 2:10 pm
by aojha
I am trying to get data from text file to Textarea.
My test text file is with 3 lines

test.txt
-------
My test file line 1

My test fine line 3


part of my - CodeFile1.php
----------------------------
......
<script language="javascript">
function GetRemarks()
{
var r;
if (r) document.body.removeChild(d);

var stud = arguments[0].options[arguments[0].selectedIndex].value;
var subjID = adminNewRemarks.sub.options[adminNewRemarks.sub.options.selectedIndex].value;
var clsID = adminNewRemarks.cls.options[adminNewRemarks.cls.options.selectedIndex].value;
var tnID = adminNewRemarks.tn.options[adminNewRemarks.tn.options.selectedIndex].value;
r = document.createElement("script");
r.src = "CodeFile2.php?info="+ stud + "&subj= " + subjID + "&ClassID=" + clsID + "&TestID=" + tnID;
r.type = "text/javascript";
document.body.appendChild(r);
}

....
....
<td>
<select id="stu" name="stu" onchange="GetRemarks(this)" style="width: 281px">
<option value=''></option>
</select>
....
...
<td style="height: 27px">
<textarea id="remark" name="remark" style="width: 437px; height: 274px"></textarea>
</td>

---------------- so when stu is selected I want to populate remark textarea
CodeFile2.php
-------------
<?php
session_start();
$schName1 = $_SESSION["Code"];
.......
.....
using sql I get $file_loc form database


$contents = "";
if($file_loc != "")
{
$filename = $file_loc;
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
}

echo "adminNewRemarks.remark.value='". $contents ."';";

include '../closedb.php';
?>

------
in text.txt file when there is only one line or more then one line without new line my code works but when there is new line in file. I get error

Error: Unterminated string constant . I see this error by clicking in bottom right of browser.

When I use <?= $_REQUEST['remark'] ?> in textarea it show whole thing in textarea in browser at run time

Re: Text file to Textarea

Posted: Fri Sep 12, 2008 7:10 pm
by ssssss
You might want to look at an Ajax tutorial or book or two. I think there are better ways to do what you're doing.

However, your error, "Error: Unterminated string constant" happens because javascript doesn't like real line endings in strings. In CodeFile2.php, replace all the line endings with '\n'. That should fix it.

Re: Text file to Textarea

Posted: Tue Sep 23, 2008 12:33 pm
by aojha
It didn’t work for me. I replace new line char to \n before saving text file but there is Small Square in front of \n in file. Don’t know how I can remove that.
If I remove that char manually then I can put that text file to textarea fine. I am working in Windows machine.

Thanks

Re: Text file to Textarea

Posted: Mon Sep 29, 2008 8:51 am
by aojha
Finally Solved.

$contents = ereg_replace("\n",'\n',$contents);

PLEASE NOTE " and ' matters.