Jump to content


Photo

Write contents of Textarea in a form to an HTML file on a server.


  • Please log in to reply
2 replies to this topic

#1 strawbshaker

strawbshaker
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 07 August 2006 - 02:17 PM

Hi There,

I am busy working on a project building my own javascript/php html editor. The javascript part is fine and dandy. I have worked out how to upload files from my hard drive to the server but what I really want to achieve is saving the contents of a form textarea into an html file to overwrite the contents of the file.

I do have the use of a MySQL database. I do want to try do it without the db though.

Can anyone tell me were to start? (ie. point me in the direction of a good tutorial or ebook or something)

Many Thanks!

#2 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,431 posts
  • LocationBelgium

Posted 07 August 2006 - 02:27 PM

http://www.phpfreaks...c,103212.0.html

you probably are gonna have to adjust the code, because this one is ment to be used for a guestbook, but it has the same cause!
<?php
$PreCheckComplete=0;
if ($_POST['submit']) {
   $Error='';

   $Name = $_POST['name'];
   if (!$Name) $Error .= "&bull; Name is a required field.<br>";
   if (strip_tags($Name) != $Name) $Error .= "&bull; You are not allowed to use html in your name.<br>";
   if (strlen($Name) < 3) $Error .= "&bull; Your name must atleast contain 3 characters.<br>";

   $Email = $_POST['email'];
   // TODO: perform an regex on email when provided.
   if ($Email) {
      if (strip_tags($Email) != $Email) $Error .= "&bull; You are not allowed to use html in your e-mail.<br>";
   }
   
   $Website = $_POST['website'];
   // TODO: perform an regex on website when provided.
   if ($Website) {
      if (strip_tags($Website) != $Website) $Error .= "&bull; You are not allowed to use html in your website address.<br>";
   }

   $Comment = $_POST['comment'];
   if (!$Comment) $Error .= "&bull; Comment is a required field.<br>";
   if (strip_tags($Comment) != $Comment) $Error .= "&bull; You are not allowed to use html in your comment.<br>";
   if (strlen($Comment) < 10) $Error .= "&bull; Please make sure your comment atleast contain 10 characters.<br>";

   if ($Error) {
      echo "<div class='Error'>$Error</div>";
   } else {
      $PreCheckComplete=1;
      $Error = '';
      $Entry = "<div class='userdetails'>Name: " . $Name;
      if ($Email) $Entry .= "<br>e-Mail: <a href='mailto:" . $Email . "'>".$Email."</a>";
      $Entry .= "</div>";
      if ($Website) $Entry .= "<a href='http://".str_replace('http://', '', $Website)."'>$Website</a>";
      $Entry .= "<div class='comment'>$Comment</div>";
      $Source = "messageview.html";
      if (!($Resource = fopen($Source, "a"))) {// a because we always want to add data on the last line..
         $Error .= "WARNING: Could not open file.<br>";
      }
      if (!flock($Resource, LOCK_EX)) {
         $Error .= "WARNING: Could not lock file.<br>";
      }
      if (!fwrite($Resource, $Entry)) {
         $Error .= "WARNING: Could not write to file.<br>";
      }
      if (!flock($Resource, LOCK_UN)) {
         $Error .= "WARNING: Could not unlock the file.<br>";
      }
      if (!fclose($Resource)) {
         $Error .= "WARNING: Could not close file.<br>";
      }
      if ($Error) {
         echo "<div class='Error'>$Error</div>";
      }
   }
}
if ($PreCheckComplete==0) {//Display the form.
?>
<form action="" method="post">
<h3>All fields marked with * are mandatory.</h3>
<table width="100%" cellpadding="0" cellspacing="0">
<tr><td>* Name:</td><td><input type="text" name="username" value="<?php echo @$_POST['username'] ? $_POST['username'] : '';?>"></td></tr>
<tr><td>E-mail:</td><td><input type="text" name="email" value="<?php echo @$_POST['email'] ? $_POST['email'] : '';?>"></td></tr>
<tr><td>website:</td><td><input type="text" name="website" value="<?php echo @$_POST['website'] ? $_POST['website'] : '';?>"></td></tr>
<tr><td>*comment:</td><td><textarea style="width:100%" rows="15" name="comment"><?php echo @$_POST['comment'] ? $_POST['comment'] : '';?></textarea></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="add to guestbook"></td></tr>
</table>
</form>
<?php } ?>


#3 strawbshaker

strawbshaker
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 07 August 2006 - 02:41 PM

Thanks ignace! I will have a play and see what I can come up with.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users