...

View Full Version : open file, rewrite it, close it, not working



croatiankid
03-12-2007, 11:29 PM
I've written this code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?
if (isset($_POST["block1"]))
{
$newcontent = $_POST["block1"];
$fp = fopen("block1.txt", "w");
fwrite($fp, $newcontent);
fclose($fp);
}
if (isset($_POST["block2"]))
{
$newcontent = $_POST["block2"];
$fp = fopen("block2.txt", "w");
fwrite($fp, $newcontent);
fclose($fp);
}
if (isset($_POST["block3"]))
{
$newcontent = $_POST["block3"];
$fp = fopen("block3.txt", "w");
fwrite($fp, $newcontent);
fclose($fp);
}
if (isset($_POST["block4"]))
{
$newcontent = $_POST["block4"];
$fp = fopen("block4.txt", "w");
fwrite($fp, $newcontent);
fclose($fp);
}
if (isset($_POST["block5"]))
{
$newcontent = $_POST["block5"];
$fp = fopen("block5.txt", "w");
fwrite($fp, $newcontent);
fclose($fp);
}
if (isset($_POST["block6"]))
{
$newcontent = $_POST["block6"];
$fp = fopen("block6.txt", "w");
fwrite($fp, $newcontent);
fclose($fp);
}
if (isset($_POST["block7"]))
{
$newcontent = $_POST["block7"];
$fp = fopen("block7.txt", "w");
fwrite($fp, $newcontent);
fclose($fp);
}
if (isset($_POST["block8"]))
{
$newcontent = $_POST["block8"];
$fp = fopen("block8.txt", "w");
fwrite($fp, $newcontent);
fclose($fp);
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>update</title>
</head>
<body>
<p>Update the text.</p>
<p>index page</p>
<form id="block1" method="post" action="update.php">
<p>
<textarea name="textarea" cols="70" rows="5"><? include("block1.txt"); ?></textarea>
<input id="save1" value="save" type="submit" />
</p>
</form>
<form id="block2" method="post" action="update.php">
<p>
<textarea name="textarea" cols="70" rows="5"><? include("block2.txt"); ?></textarea>
<input id="save2" value="save" type="submit" />
</p>
</form>
<form id="block3" method="post" action="update.php">
<p>
<textarea name="textarea" cols="70" rows="5"><? include("block3.txt"); ?></textarea>
<input id="save3" value="save" type="submit" />
</p>
</form>
<form id="block4" method="post" action="update.php">
<p>
<textarea name="textarea" cols="70" rows="5"><? include("block4.txt"); ?></textarea>
<input id="save4" value="save" type="submit" />
</p>
</form>
<p>under construction page</p>
<form id="block5" method="post" action="update.php">
<p>
<textarea name="textarea" cols="70" rows="5"><? include("block5.txt"); ?></textarea>
<input id="save5" value="save" type="submit" />
</p>
</form>
<form id="block6" method="post" action="update.php">
<p>
<textarea name="textarea" cols="70" rows="5"><? include("block6.txt"); ?></textarea>
<input id="save6" value="save" type="submit" />
</p>
</form>
<form id="block7" method="post" action="update.php">
<p>
<textarea name="textarea" cols="70" rows="5"><? include("block7.txt"); ?></textarea>
<input id="save7" value="save" type="submit" />
</p>
</form>
<form id="block8" method="post" action="update.php">
<p>
<textarea name="textarea" cols="70" rows="5"><? include("block8.txt"); ?></textarea>
<input id="save8" value="save" type="submit" />
</p>
</form>
</body>
</html>

which doesn't work. Tried editing the first text area and clicked save, it didn't save, no php error. Please get back to me quickly, thanks a ton!

Fumigator
03-12-2007, 11:35 PM
<textarea name="textarea" cols="70" rows="5"><? include("block1.txt"); ?></textarea>


That doesn't work like you want it to. You have to open and read the file and use echo statements to get it into your HTML markup.

I like the file() (http://us2.php.net/file) function for this, easy and fast to load a file's contents to an aray. Then simply use a foreach() loop to echo each line.

croatiankid
03-12-2007, 11:37 PM
are you sure? because, just for your info, the contents of block1.txt show up in the text area. it's just that it doesn't seem to save it. or is it necessary if i want to save it? i don't understand.

Nightfire
03-12-2007, 11:47 PM
You've not named the textareas


<textarea name="textarea" cols="70" rows="5" name="block1"><? include("block1.txt"); ?></textarea>

croatiankid
03-12-2007, 11:51 PM
wow thanks, for some reason I gave an id to the form itself, not the actual textarea, which I'm targeting lol



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum