0

I have the code to a simple form page. It writes to a file and loads the content with include() but it doesn't work. Any of you could tell me where am I mistaken?

  • The error is that it doesn't write down to the file anything at all, the form submits and nothing happens.
  • I have administrator rights (this happens in local AND hosting).
  • Tested the include() and works perfectly.

Sorry for the lack of details. :(

The code is:

<!DOCTYPE html>
<?php

if (isset($_POST) && isset($_POST['msg'])) {

    $msgfile= 'msg.html';
    $msg = $_POST['msg'];
    $msgdetails =  '<p><span>'. date("F j, Y, g:i a") .'</span>: '. $msg .'<br>\n';
    $fp = fopen($msgfile, "a"); 
    fwrite($fp, $msgdetails);
    fclose($fp); 

}

?>

<form method="post" action="#">

    <table>
        <tr><td><h3>Messages</h3></td></tr>
        <tr>
            <td>
                <input type="text" id="msg">
                <input type="submit" value="Add message!">
            </td>
        </tr>
        <tr><td><?php include('msg.html'); ?></td></tr>
    </table>

</form>

Well the error was in the , the NAME param was missing (as mentioned below). Thank you! :)

MoeSzislak
  • 137
  • 1
  • 4
  • 13
  • I would at least use `htmlspecialchars` when outputting that `$msg` to prevent XSS. And *what* doesn't work? The opening of the file? Writing to it? Including it in your markup? – Marcel Korpel May 27 '13 at 20:44
  • Yet another `it doesn't work` type, without reading the manual where it tells you what `include`does.. – N.B. May 27 '13 at 20:47
  • Do you have permission to write to `msg.html`? – Marcel Korpel May 27 '13 at 20:51
  • Doesn't work how? doesn't write the contents? file stays blank? wrong contents written? – Marc B May 27 '13 at 20:53
  • I am sorry. ok? Now, It doesn't write down to the file, THAT is the error (but without anything poping up). The include works 100% fine, tested it. ANd the privileges, YES I have, both the machine and I. I am testing it with local (Wamp) and HOST, and both are failing. – MoeSzislak May 27 '13 at 20:53
  • 2
    I noticed that you don't have a 'name="msg"' attribute on your text input -- just an id. – ryanlahue May 27 '13 at 21:01
  • 1
    And `\n` isn't interpreted when using single quotes. – Marcel Korpel May 27 '13 at 21:11
  • name attribute was missing, both right. Thank you!! About the \n, note taken! – MoeSzislak May 27 '13 at 21:21

1 Answers1

1

Put a name field in

 <input type="text" id="msg" name="msg" />

rest of your code works fine. $_POST['msg'] reads the submitted name not the ID

MTahir
  • 1,173
  • 1
  • 13
  • 25