1

Using PHP I want to store user input from an html page in a .txt file and output that user input onto the same page.

However, nothing is showing up in either the .txt file or on the page div.

IN ADDITION: I'd like to have each new message display on a new div, I know this is somewhat of a different question, but I figured it could be related.

Any ideas on why this isn't working?

HTML:

The form itself:

<textarea name="msg" rows="5"></textarea>
<form id="post" name="post" action="storeText.php" method="post">
    <input class="button" type="submit" value="POST!"/>
</form>

Where I want to output on the same html page:

<div class="menuItem" >
    <?=$msg?>
</div>

PHP:

storeText.php

<?php       
$filename = 'posts.txt';
$msg = (isset($_POST['msg']) ? $_POST['msg'] : null);

    // Let's make sure the file exists and is writable first.
    if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
        echo "Cannot open file ($filename)";
        exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $msg) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($msg) to file ($filename)";

    fclose($handle);

    } else {
    echo "The file $filename is not writable";
    }
?>
18
  • Where in your form are you inputting text? Commented Sep 12, 2013 at 23:00
  • Woops... that could be my problem... my textarea is outside of the form... Commented Sep 12, 2013 at 23:01
  • I updated the html to reflect what it is currently. I suspect I need to put the textarea inside of the form, correct? Commented Sep 12, 2013 at 23:02
  • Where does $filename come from? Commented Sep 12, 2013 at 23:04
  • 1
    @Keven Add this just under $filename = 'posts.txt'; - chmod($filename, 0644); or use 0777 may help to solve file writing permissions if that is the case. Commented Sep 12, 2013 at 23:18

3 Answers 3

2

Give this a try.

There are two files, one for the submission(s) which posts from the same page and the DIV include display_div.php.

The way it's setup is that it will show the newest post on top.

PHP handler and form

<?php       
// check if the file first exists, if not create the file
if (!file_exists("posts.txt")) {
$fp = fopen('posts.txt', 'a+');
fwrite($fp, '');
chmod("posts.txt", 0644);

// you may have to use this one
// remember to comment out the one with 0644 if using this one
// chmod("posts.txt", 0777);
fclose($fp);
}

// check if "msg" is set
if(isset($_REQUEST['msg'])) {
$file = 'posts.txt';  
$str = $_POST['msg'] . "\n";
$temp = file_get_contents($file);
$content = $str.$temp;
file_put_contents($file, $content);

// echo success message
echo "Saved to $file successfully!";
echo "<br>";
echo "Previous messages:";
echo "<hr>";

// print out previous posts
$file = file_get_contents('posts.txt', true);
$lines = explode(PHP_EOL, $file);

foreach ($lines as $line) {
    if (strpos($line, '/') === false) {
        $line = htmlspecialchars($line . "\n");
        echo nl2br($line);
    }
}
exit;
} // end of if(isset($_REQUEST['msg']))

?>

<form id="post" name="post" action="" method="post">
<textarea name="msg" rows="5"></textarea>
<br>
<input class="button" type="submit" name="submit" value="POST!" />
</form>

<?php include 'display_div.php'; ?>

display_div.php

N.B.: You can get rid of Previous messages:<br> or replace it with your text.

<div class="menuItem" >
Previous messages:<br>
<?php
$file = file_get_contents('posts.txt', true);
$lines = explode(PHP_EOL, $file);

foreach ($lines as $line) {
    if (strpos($line, '/') === false) {
        $line = htmlspecialchars($line . "\n");
        echo nl2br($line);
    }
}
?>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Hey Fred. This has been helpful. I didn't copy it exactly, but I am in the process of mimicking the logic. In particular, I wanted to store and then print out a post line-by-line, so this has helped with that. I appreciate it! PS- I got it to post to the same page, but I am enhancing it now.
1

Change your HTML markup to the following:

<form id="post" name="post" action="storeText.php" method="post">
    <textarea cols="x" rows="x" name="msg"></textarea>
    <input class="button" type="submit" value="POST!"/>
</form>

You should now be able to write data to the file, provided the file has correct permissions and PHP has write access.

5 Comments

Guess what. I'm gonna have to take this check away... editing the if statement broke it...
I stole this script directly from PHP: php.net/manual/en/function.fwrite.php Once I changed it back to the way they had it, it worked.
You answered my original question. I'll leave the check there. Well, ALMOST all of it. I still can't output to the page...
@AmalMurali I hit submit and the script shows up. I want the content to load on the same page, but it isn't.
@Keven: In that case, you could submit the form to itself.
0

You seems to be missing an input.Please see below

<form id="post" name="post" action="storeText.php" method="post">
<textarea name="msg" cols="" rows=""></textarea>
<input class="button" type="submit" value="POST!"/>
</form>

Thanks

2 Comments

Yep, I realize I accidentally put the textarea outside of the form.
It looks like the php is running, but I can't open my file. Is that a security issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.