Search code examples
phpformspostfwrite

Writing to a file and loading content / suggestions-like


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! :)


Solution

  • 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