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
Text file to Textarea
Moderator: General Moderators
Re: Text file to Textarea
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.
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
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
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
Finally Solved.
$contents = ereg_replace("\n",'\n',$contents);
PLEASE NOTE " and ' matters.
$contents = ereg_replace("\n",'\n',$contents);
PLEASE NOTE " and ' matters.