Why cant i save the form?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Antila
Forum Newbie
Posts: 3
Joined: Sun Oct 19, 2008 6:29 am

Why cant i save the form?

Post 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>
yoji
Forum Commoner
Posts: 25
Joined: Sun Oct 19, 2008 3:09 am

Re: Why cant i save the form?

Post 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
Antila
Forum Newbie
Posts: 3
Joined: Sun Oct 19, 2008 6:29 am

Re: Why cant i save the form?

Post 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...
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Why cant i save the form?

Post 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.
Antila
Forum Newbie
Posts: 3
Joined: Sun Oct 19, 2008 6:29 am

Re: Why cant i save the form?

Post 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>
Post Reply