Jump to content

Reading file in a textbox and replacing the information within it after saving


Recommended Posts

From what I have so far is that I am reading a text file into a textbox.  Now I want to update/append new information to my file(the information may contain HTML/PHP code, text etc.) after I click the save button so the contents can be read on a separate page called "edit.php".  Any suggestions?  (My first time using PHP on a higher level)

 

The following is written in my editcontent1.php(I'm seperating each content block on my edit.php page into several editcontent(n).php files where n is = 1 to infinity)

 

Here is before the html tags:

<?php
if ( isset ($_POST ['content']) )
{
    file_put_contents ('content1.txt', $_POST ['content'] );
}
?>

 

Here is before the html tags:

<form action = "edit.php" method="post">
<textarea name="content" cols="" rows="" wrap="virtual" class="textarea1">
<?php include 'content1.txt'; ?>
</textarea>
<br />
<input type="submit" value="Save">
</form>

 

Something like this might help...

<?php

$file = 'mytextfile.txt';
$data = $_POST['content'];

if($_SERVER['REQUEST_METHOD'] == "POST")
{
//update the text file
$fp = fopen($file, 'w+');
fwrite($fp, $data);
fclose($fp);

?>
	<form action = "<?php echo $_SERVER['PHP_SELF'];?>" method="post">
	<textarea name="content" cols="" rows="" wrap="virtual" class="textarea1">
	<?php include $file; ?>
	</textarea>
	<br />
	<input type="submit" value="Save">
	</form>
<?

}
else
{
?>

	<form action = "<?php echo $_SERVER['PHP_SELF'];?>" method="post">
	<textarea name="content" cols="" rows="" wrap="virtual" class="textarea1">
	<?php include $file; ?>
	</textarea>
	<br />
	<input type="submit" value="Save">
	</form>

<?
}

 

 

I see nothing wrong with the OP's code, just a little more sanitation.

 

Although, you may want to re-think how you are going about this.  Such as:

 

1. Why do you need to have a form to create php code? You could just create/append/edit it in an editor, then ftp it to your site.

 

2. Why not use a database?  Easier to maintain, and you wouldn't have ~infinity files.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.