Home > OS >  php file_put_contents() update from textarea problem
php file_put_contents() update from textarea problem

Time:03-09

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']);
}
    ?>

CodePudding user response:

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>

CodePudding user response:

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>
  •  Tags:  
  • php
  • Related