Thursday, 12 September 2013

PHP: Input to .txt AND html page

PHP: Input to .txt AND html page

So, I got my PHP script to store input in a .txt file, however, something
is not working with my html page. I want to output text to both a .txt
file as well as a div on my html page.
I personally think it has to do with the fact that the script itself is on
a separate page and I'm refreshing the page so there is never an
opportunity to show up. I feel like what the PHP script should do is pull
from the .txt file instead. But not sure.
I've tried action="" and putting the PHP on the same page but that didn't
work. The ONLY way it has worked is when I do action="[php file]".
Any ideas?
HTML:
The reference:
<?php require_once('storeText.php');?>
The Form:
<form id="post" name="post" action="storeText.php" method="post">
<textarea name="msg" rows="5"></textarea>
<input class="button" name="submit" type="submit" value="SQUAWK!"/>
</form>
Where I want the input to go on the page:
<div class="menuItem" >
<?php echo $msg; ?>
</div>
PHP:
storeText.php
<?php
if(isset($_POST['submit']))
{
$filename = 'posts.txt';
$msg = (isset($_POST['msg']) ? $_POST['msg'] : null);
if(is_writable($filename))
{
if($handle = fopen($filename, 'a'))
{
fwrite($handle, $msg);
echo "Success, wrote ($msg) to file ($filename)";
fclose($handle);
}
else
{
echo "Could not open file.";
}
}
else
{
echo "File not writable.";
}
header("Location: profile.html");
}
?>

No comments:

Post a Comment