Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Textarea Editor (0 Comments)
Admin: Posted Date: April 4, 2010

With this tutorial, you will be able to edit files online through a textarea form. This allows you to speed up the process of editing content on your website to help make it dynamic.

Textarea Editor

Seems more and more people are wanting to edit files on-line.
also a common problem in on-line editors is the textarea because editors on-line use the same code,
so it breaks the editor.

first thing is to point it to a file to open,
this part can be done many ways,
but this tutorial does not address those ways or makeing it load different files.

file to open and open it in read mode.
<?php
$loadcontent = "path/file_name.txt"; 
        $fp = @fopen($loadcontent, "r");
                $loadcontent = fread($fp, filesize($loadcontent));
                $loadcontent = htmlspecialchars($loadcontent);
                fclose($fp);
?>

Now we need a way to to view what we just opened.
So we build a form with textarea to make editable content with a button to save it.

<form method=post action="<?=$_SERVER['PHP_SELF']?>">
<textarea name="savecontent" cols="70" rows="25"><?=$loadcontent?></textarea>
<br>
<input type="submit" name="save_file" value="Save">  
</form>

Function placement? full code below
<?php
        if($save_file) {
                $savecontent = stripslashes($savecontent);
                $fp = @fopen($loadcontent, "w");
                if ($fp) {
                        fwrite($fp, $savecontent);
                        fclose($fp);
}
}
?>
so now we have all the parts we need just need them in order so.....


complete code,
<?php
$loadcontent = "file_name.txt"; 
        if($save_file) {
                $savecontent = stripslashes($savecontent);
                $fp = @fopen($loadcontent, "w");
                if ($fp) {
                        fwrite($fp, $savecontent);
                        fclose($fp);
                                                           }
                                }
        $fp = @fopen($loadcontent, "r");
                $loadcontent = fread($fp, filesize($loadcontent));
                $loadcontent = htmlspecialchars($loadcontent);
                fclose($fp);
?>
<form method=post action="<?=$_SERVER['PHP_SELF']?>">
<textarea name="savecontent" cols="70" rows="25"><?=$loadcontent?></textarea>
<br>
<input type="submit" name="save_file" value="Save">  
</form>
if you have problems with the server register globals being off,
is 2 ways to fix this.
1) add at the top for all the post feilds in the form, 
$save_file = $_POST['save_file'];  
$savecontent = $_POST['savecontent'];
for each one.
2) or change them all to ,
        if($_POST['save_file']) {
                $savecontent = stripslashes($_POST['savecontent']);


 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: