Display String Array to Textarea

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aojha
Forum Newbie
Posts: 9
Joined: Wed Sep 10, 2008 6:35 am

Display String Array to Textarea

Post by aojha »

Hi Guys,

This is my first post and I am new to php. This is what I am not able to do and looking for some sample code.
I have a html Textarea, user enter some comments and I save that comments to text file or I can save to mysql too but for now I am creating text file.

Next time when user comes and like to update comments. I am not able to bring text from text file to Textarea IF that file has NEW LINE.
I have postback when user select some thing in page to load comments to textarea and code I am trying following....

$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$contents = fread($fh, filesize($myFile));
fclose($fh);
echo "formName.textareaName.value='" . $contents . "';";

when I don't have new line in "textFile.txt" it's fine but when there is new line, comments doesn't come... I also tried using fgets with loop... or file("testFile.txt");

Thanks for your help - Ashok
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: Display String Array to Textarea

Post by marcth »

I'm not sure I understand your post, but if $content is an array you can do the following:

Code: Select all

 
$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$contents = fread($fh, filesize($myFile));
fclose($fh);
$contents = implode("\n", $contents);
 
echo "formName.textareaName.value='" . $contents . "'";
 
aojha
Forum Newbie
Posts: 9
Joined: Wed Sep 10, 2008 6:35 am

Re: Display String Array to Textarea

Post by aojha »

Thats right?

My question is how can I display contents in text file to html Textarea? Also when the text file has new line in it.

Thanks
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: Display String Array to Textarea

Post by marcth »

Is this what you want?:

Code: Select all

 
function readTextFile($fileName) {
  $filename ='';
  $handle = fopen($filename, 'r');
  $contents = fread($handle, filesize($filename));
  fclose($handle);
  
  return $contents;
}
 
if(!isset($_REQUEST['notes'])) {
  $_REQUEST['notes'] = trim(readTextFile('testFile.txt'));
}
 

Code: Select all

 
<textarea name="notes"><?= $_REQUEST['notes'] ?></textarea>
 
aojha
Forum Newbie
Posts: 9
Joined: Wed Sep 10, 2008 6:35 am

Re: Display String Array to Textarea

Post by aojha »

I am not able to make it work because I am very new to php
so again..

I have first php page with drop down box and textarea. I changed my text area to like this

<textarea name="notes"><?= $_REQUEST['notes'] ?></textarea>

when drop down item is selected I call Java script and in java script I call second php file in that file I am using this code.
I am calling second php file because I need to get path of the file from database.

$filename ='test.txt';
$handle = fopen($filename, 'r');
$contents = fread($handle, filesize($filename));
fclose($handle);

$_REQUEST['notes'] = trim($contents);

but file text is not coming to textarea. some thing I am not using right...
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: Display String Array to Textarea

Post by marcth »

Code: Select all

 
 
$filename ='test.txt';
$handle = fopen($filename, 'r');
$contents = fread($handle, filesize($filename));
fclose($handle);
 
$_REQUEST['notes'] = trim($contents);
 
print "***<pre>";
print_r($_REQUEST['notes']);
print "<pre>"***";
 
What does this output? What's the contents of the TXT file?
aojha
Forum Newbie
Posts: 9
Joined: Wed Sep 10, 2008 6:35 am

Re: Display String Array to Textarea

Post by aojha »

Sorry I should have put that, my test.txt has 3 lines like this. so it has new line char in it...

test.txt
--------
This is my test line 1.

This is my test line 3.

I tried it didn't worked. My code is below.

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.
aojha
Forum Newbie
Posts: 9
Joined: Wed Sep 10, 2008 6:35 am

Re: Display String Array to Textarea

Post by aojha »

Finally Solved.

ereg_replace("\n",'\n',$var);

Please note " and ' matters.
Post Reply