-4

Let's say user1 writes "Hello" in a textbox and submits it, and user2 writes later that day " World" and submits it. How can i code it so their inputs get sent to my .txt file? So the .txt file should look like this: "Hello World". This might be a bit complicated to answer. If so, could anyone point me in the right direction or give me any good links / tutorials? And second question: Do I need to learn MySQL for this?

ArturO_O
  • 1
  • 1
  • `file_put_contents` with `FILE_APPEND` might be useful. As for `Do I need to learn MySQL` - IMO You would be better using a database than a textfile perhaps but that is an opinion based question/answer – Professor Abronsius Mar 25 '21 at 18:11
  • Welcome to StackOverflow! Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Rojo Mar 25 '21 at 18:14
  • Please read [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). If you just do some research for each step (like "forms in php", "store text in file with php" and so on) you would find _many many_ tutorials. And Mysql is a database and doesn't have anything to do with what you're asking (even though I agree with the first comment, it would be preferred to use a database over text files) – M. Eriksson Mar 25 '21 at 18:18

1 Answers1

0

It might help...

  • index.php

    <?php
    if (!isset($_SESSION)) {
        session_start();
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
    <?php
    if (!empty($_SESSION)) {
        echo $_SESSION['msg'];
        session_unset();
    }
    ?>
    <form action="store.php" method="POST">
        <input type="text" name="inputText">
        <input type="submit">
    </form>
    </body>
    </html>

  • store.php
    <?php
    include_once 'TextStoreIntoFile.php';
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $addText = new TextStoreIntoFile();
        $addText->addTextIntoFile($_REQUEST['inputText']);
    }
  • TextStoreIntoFile.php
    <?php
    declare(strict_types=1);

    class TextStoreIntoFile
    {
        public function addTextIntoFile(string $data)
        {
            if (!isset($_SESSION)) {
                session_start();
            }
            $data = $data . ' ';
            $textAdded = file_put_contents('text.txt', $data, FILE_APPEND | LOCK_EX);
            if ($textAdded) {
                $_SESSION['msg'] = 'Text added into the file';
                header('Location: index.php');
            } else {
                $_SESSION['msg'] = 'Text not added!';
                header('Location: index.php');
            }
        }
    }