Page 1 of 1

Why cant i save the form?

Posted: Sun Oct 19, 2008 6:48 am
by Antila
I´m trying to build an function that opens a page to edit in a form, but im totaly stuck now. I will try to open it with GET-command, and it worked, but after editing i cant save it. Can you see what the problem is?

Code: Select all

<?php
$filename = $_GET["page"];
$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);
?>
 
<?=$msg?>
<form method="post" action="page.php">
<textarea name="content" cols=58 rows=20><?=$content?></textarea>
<input type="submit" value="Save" name="submitbtn">
</form>
</td>

Re: Why cant i save the form?

Posted: Sun Oct 19, 2008 6:52 am
by yoji
I am not a pro myself but I think the problem is because you haven't put any braces in your conditional statements.
If (condition)
{
//code
}
Place braces like this in each of your conditions.. I think it might just work

Re: Why cant i save the form?

Posted: Sun Oct 19, 2008 10:33 am
by Antila
No, i dont think its that. It works if i change "$filename = $_GET["page"]; to "$filename = "example.txt"];

It´s the GET-think that messed it up...

Re: Why cant i save the form?

Posted: Sun Oct 19, 2008 11:15 am
by onion2k
You're not setting a GET variable called "page" anywhere. Variables aren't remembered between pages, so if you load page.php?page=value ... that variable will not be available the next time the page is loaded ... eg when the form is submitted.

Re: Why cant i save the form?

Posted: Sun Oct 19, 2008 12:12 pm
by Antila
Ah, i get it. I newer thought about that.

Edit: After a few tryes i finaly got it to work. Thanks!

Code: Select all

<?php
$filename = $_GET["page"];
$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);
 
?>
 
<?=$msg?>
<form method="post" action="page.php?page=<?=$filename;?>" >
<textarea name="content" cols=58 rows=20><?=$content?></textarea>
<input type="submit" value="Save" name="submitbtn">
</form>
</td>