DotNetRussell
DotNetRussell

Reputation: 9857

How to fill a TextArea with HTML code inside a form

So I built a site last night and now I want to add in the functionality of being able to edit the files from the web. To do a proof of concept I created:

  1. A list box filed with file names in the DIR (Working)

  2. A submit button that calls the selected file (Working)

  3. A text area to display the code (Working kinda)

  4. A save button (not hooked up yet)

I can't figure out how to fill the text area with a HTML file that contains <form> tags. As soon as I do it breaks the actual form on the page and messes up all the ui. This is just a prototype but I can only imagine the real site will have a similar issue.

Here is the relevant code for your testing purposes.

<html>
<body>
  <form action="getdir.php" method="GET">
   <table>
    <td valign="top">
     <select name="file">
     <option value=""></option>
     <?php
       $size = 0;    
        foreach(glob("*.html") as $filename)
        {
          $size++;
        }
       $count = 0;
       $files[size];

        foreach(glob("*.html") as $filename)
        {
         $files[$count] = $filename;
         print('<option value=');
         print($filename);
         print('>');
         print($filename);
         print('</option>');
         $count++;
        }
     ?>
     </select>
    <input type="submit" value="Get Code"/></td><td>
</form>

<form action="getdir.php" method="POST">
<?php     
 $f = $_GET['file'];
 if($f!=null){
    $openedFile = fopen($f,'r');
    $read = fread($openedFile,filesize($f));
    print('<textarea name="tb1" rows="100" cols="100">');
    print(addslashes($read));
    print('</textarea></td>');
    fclose($openedFile);
  }
 else{
   print('<textarea name="tb1" rows="100" cols="100"></textarea></td>');
  }
?>
<td>
   <input type="submit" style="height:800px" value="Save Code"/>
</td>
</form>   
</body>
</html>

Upvotes: 0

Views: 1768

Answers (1)

Steely Wing
Steely Wing

Reputation: 17607

Try replace addslashes with htmlspecialchars

Upvotes: 3

Related Questions