Jump to content

Editing and overwritting data in .txt file


Schlo_50

Recommended Posts

I have a page whereby a text area is shown, and the initial value of it uses php to display the contents of the .txt file so that a user to update the paragraph of text shown and click submit to overwrite the .txt file with new information. The problem is i have no idea how to code it so that i can make the .txt file be over written.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
background-color: #6191c2;
}
-->
</style>
<SCRIPT language="Javascript">
function selectit(input){
document.formname.news.value+=input
}
</SCRIPT>


</head>

<body>
<label> 
<p><span class="rotary"><strong>Text Editor </strong></span><br />
  <br />
//This part here is where i have tried to make an over write.
  <?php 
if ($_POST['submit'] == "Submit")
{
$file_name = "home.txt";
$open_file = fopen($file_name, "w");
$file_contents= $_POST['title'] . '|' . $_POST['news'] ."\n";
fwrite($open_file, $file_contents);
fclose($open_file);
echo "Form data successfully written to file";
}
?>
</p>
<p>
  Title:
  <label>
  <input type="text" name="title" />
  </label>
</p>
<p>  
  <?php

$prnt = "<img border=\"0\" src=\"images/bold.jpg\" width=\"25\" height=\"25\" alt=\"Bold\" onClick=\"selectit('<b>Text Here</b>')\"> ";

$prnt .= "<img border=\"0\" src=\"images/underline.jpg\" width=\"25\" height=\"25\" alt=\"Underline\" onClick=\"selectit('<u>Text Here</u>')\"> ";

$prnt .= "<img border=\"0\" src=\"images/link.jpg\" width=\"25\" height=\"25\" alt=\"Hyperlink\" onClick=\"selectit('<url=http://www.site.com>Site Name</url>')\"> ";
?>
  <?php
echo("$prnt");
?>
  <br />
</p>
<form name="formname">
  <p>
    <textarea name="news" cols="85" rows="15" id="news">

<?php
  
  $file = file("home.txt");
  foreach($file as $key => $val){
  $data[$key] = explode("|", $val);
  
  $text = $data[$key][0];
  
   print( $text );

}


?>
  </textarea>
</p>
  <p>
    <label>
    <input type="submit" name="submit" value="Submit" />
    </label>
</p>
</form>
</label>
</body>
</html>

 

Please Help!!

Thanks

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.