Page 1 of 1

edit page with a form and php

Posted: Sat Oct 18, 2008 3:03 pm
by 930d
<form method="post" action="index.php?saving=1">
<textarea name="content" style="width:100%"><?php include "content.php"?>
</textarea>
<input type="submit" value="Save">
</form>
<?php
$saving = stripslashes($_REQUEST['saving']);
$content = stripslashes($_POST['content']);
$file = "content.php";
$fp = fopen($file, "w") or die("Couldn't open $file for writing!");

if ($saving == 1){
fwrite($fp, $content) or die("Couldn't write values to file!");
fclose($fp);
echo "Saved to $file successfully!";
}
?>

I am stuck at this code.
It should be a page where you can edit a certain text file. The text should be included in the form as initial value, and saved/modified when hit save button.
Can someone help, i have no clue anymore and I'm pulling my hair !
I guess it must be conditioned with save==1 or ==0 , to not erase everything, just include the file (content.php in this case) to be modified then included again !??!
Thanks a lot for any answers !

Re: edit page with a form and php

Posted: Sat Oct 18, 2008 3:28 pm
by yacahuma

Code: Select all

 
<?php
$filename = 'data.txt';
$content  = '';
$msg      = '';
 
if (isset($_POST['submitbtn']))
 $msg = (file_put_contents($filename,$_POST['content'])) ? "Saved successfully!" : "Error saving content";
 
if (file_exists($filename))
  $content = file_get_contents($filename);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Page title</title>
</head>
<body>
<?=$msg?>
<form method="post" action="test.php">
<textarea name="content" style="width:100%"><?=$content?></textarea>
<input type="submit" value="Save" name="submitbtn">
</form>
</body>
</html>
 
 

Re: edit page with a form and php

Posted: Sat Oct 18, 2008 4:48 pm
by 930d
Thank you !
This is great.