-1

so i tried to make textarea that update other page, the code and its not wroking, i tried changing few things and still not working

    <form>
    <textarea name="writing"> <?php echo file_get_contents('thetext.html') ?></textarea>
        <input type="submit" name="submitsave" value="Save">
        </form>

    <?php
if (isset($_POST['submitsave']))
{
    file_put_contents('thetext.html', $_POST['writing']);
}
    ?>
wow ok
  • 9
  • 1
  • 1
    "it's not working" - please explain in more detail what's not working. Are you getting any error messages? Can you confirm the value of `$_POST['writing']` is what you're expecting? Is your form even submitting? Is `isset($_POST['submitsave'])` true and it's actually entering the conditional? – WOUNDEDStevenJones Mar 08 '22 at 21:46
  • sorry, the "not working" part is that after pressing the save button its not changing the text i wrote in the textarea at the "thetext.html" – wow ok Mar 08 '22 at 21:49
  • So the way that this is theoretically working is you load a form on a page with the contents of `thetext.html`. Then you can edit this text and submit the form. Then the page will reload, where the PHP will be processed. The issue is that you're first echoing `file_get_contents` _before_ you're doing `file_put_contents`. Does this work if you move the `` to the top of the page (or anywhere before `
    `)?
    – WOUNDEDStevenJones Mar 08 '22 at 21:51
  • yea i tried this one, but this still not working, thats why i asked it here, maybe there is something that i missed – wow ok Mar 08 '22 at 22:01
  • Ah, does this work if you change your form to `
    `? https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-method You're currently not POSTing any data. https://stackoverflow.com/questions/2314401/what-is-the-default-form-http-method
    – WOUNDEDStevenJones Mar 08 '22 at 22:07

2 Answers2

1

so ifixed it, here the code

<form action="" method="post">
    <textarea name="writing"><?php echo file_get_contents('thetext.html')?>    </textarea>

        <?php
if (isset($_POST['submitsave']))
{
    file_put_contents('thetext.html', $_POST['writing']);
}
    ?>
        <input type="submit" name="submitsave" value="Save">
   </form>
wow ok
  • 9
  • 1
  • I still don't think this will work as expected, because of the order of `file_get_contents` and `file_put_contents`. You'll always be echoing the old value before `thetext.html` is updated. You'll want to move the `file_put_contents` first. – WOUNDEDStevenJones Mar 08 '22 at 22:16
  • thanks i took it up so it update after clicking the button – wow ok Mar 08 '22 at 22:27
0

Make sure you have method="post" on your <form> in order for $_POST to work. Additionally, you'll want to move your "update" logic to the top of the page so that gets handled first when the form is submitted, and then the file is read back into the <textarea>. Otherwise, you'll submit the form, it will read the (old) value back into the textarea, and then it will update the file with the new value.

<?php
    if (isset($_POST['submitsave']))
    {
        file_put_contents('thetext.html', $_POST['writing']);
    }
?>

<form action="" method="post">
    <textarea name="writing">
        <?php echo file_get_contents('thetext.html')?>
    </textarea>

    <input type="submit" name="submitsave" value="Save">
</form>
WOUNDEDStevenJones
  • 5,150
  • 6
  • 41
  • 53