Save form data in TXT (PHP)

Hello, good afternoon! I have the following code:

    <?php

if (isset($_POST['texto'])) {
   $texto = $_POST['texto'];

   $arquivo = fopen('msg.txt', 'w');
   fwrite($arquivo, $texto);
   fclose($arquivo);
}
?>

That should work along with this:

<form method="post">
             <span style="font-family:lucida sans unicode,lucida grande,sans-serif">login:</span>
               <input type="text" name="texto" style="height: 40px">
             </span>Senha:</span>
            <input type="password" name="texto" style="height: 40px"><br></font>
            <button style="width:90px, height:40px">Enviar</button></center>
         </form>

Why, he doesn't write the password also in TXT?? it only saves one form field in TXT.

I would like the code to save up all the form fields in TXT, and I would also like to do the following, it will save msg.txt ... when the next person is going to make him identify that there is already an msg.TXT and create a msg2.txt to not overwrite the first, How do you do it??

Author: Carlos, 2020-08-20

2 answers

Why, he doesn't write the password also in TXT??

To get all the values of the inputs that have the same name you need to add brackets in the attribute name example name="texto[]", in this way when arriving in PHP these fields will arrive as an array.

<input type="text" name="texto[]" style="height: 40px">
<input type="password" name="texto[]" style="height: 40px">

You can also give different names for the fields, example: login and senha

<input type="text" name="login" style="height: 40px">
<input type="password" name="senha" style="height: 40px">

In the first case retrieves values submitted from the form and creates the contents in this way

// cria conteúdo para escrever no arquivo de texto
$textoPublicar = "login: ".$_POST['texto'][0]. " Senha: ".$_POST['texto'][1];

In the second case thus

// cria conteúdo para escrever no arquivo de texto
$textoPublicar= "login: ".$_POST['login']." senha: ".$_POST['senha'];

Complete commented code for the first case

<?php

    //mesma pasta
    $dir="";
    
    //outra pasta, caminho da pasta, exemplo
    $dir="textos/";
    
    // glob() - Retorna um array contendo todos os arquivos (*) com extensão .txt do diretorio indicado
    $array = glob($dir."*.txt");
    
    // se for um array e não vazio
    if ( is_array($array) && !empty($array) ) {
        $numeros=[];
            foreach ( $array as $val) {
                //cria um array somente com a parte numérica dos nomes dos arquivos
                $numeros[] = preg_replace("/[^0-9]/", "", $val);
            }
        
        // max() recupera o maior valor do array $numeros
        // cria o numero do proximo nome do arquivo de texto
        $numArquivo = max($numeros)+1; 
    
    }
    
    //cria o nome do arquivo de texto
    $filename = "msg".($numArquivo).".txt";
    
    
    if ( isset($_POST['texto']) && !empty($_POST['texto']) ) {
    
        // cria conteudo para escrever no arquivo de texto
        $textoPublicar = "login: ".$_POST['texto'][0]. " Senha: ".$_POST['texto'][1];
       
       //cria o arquivo e grava
       $arquivo = fopen($dir.$filename, 'w');
       fwrite($arquivo, $textoPublicar);
       fclose($arquivo);
       
    }

?>

<form method="post">
    <span style="font-family:lucida sans unicode,lucida grande,sans-serif">login:</span>
    <input type="text" name="texto[]" style="height: 40px">
    </span>Senha:</span>
    <input type="password" name="texto[]" style="height: 40px"><br></font>
    <button style="width:90px, height:40px">Enviar</button></center>
</form>

Complete commented code for the second case

<?php

    //mesma pasta
    $dir="";
    
    //outra pasta, caminho da pasta, exemplo
    $dir="textos/";
    
    // glob() - Retorna um array contendo todos os arquivos (*) com extensão .txt do diretorio indicado
    $array = glob($dir."*.txt");
    
    // se for um array e não vazio
    if ( is_array($array) && !empty($array) ) {
        $numeros=[];
            foreach ( $array as $val) {
                //cria um array somente com a parte numérica dos nomes dos arquivos
                $numeros[] = preg_replace("/[^0-9]/", "", $val);
            }
        
        // max() recupera o maior valor do array $numeros
        // cria o numero do próximo nome do arquivo de texto
        $numArquivo = max($numeros)+1; 
    
    }
    
    //cria o nome do arquivo de texto
    $filename = "msg".($numArquivo).".txt";

    if ( isset($_POST['login']) && isset($_POST['senha']) ) {
        
       // cria conteudo para escrever no arquivo de texto
        $textoPublicar= "login: ".$_POST['login']." senha: ".$_POST['senha'];
       
       //cria o arquivo e grava
       $arquivo = fopen($dir.$filename, 'w');
       fwrite($arquivo, $textoPublicar);
       fclose($arquivo);
       
    }
    
?>

<form method="post">
    <span style="font-family:lucida sans unicode,lucida grande,sans-serif">login:</span>
    <input type="text" name="login" style="height: 40px">
    </span>Senha:</span>
    <input type="password" name="senha" style="height: 40px"><br></font>
    <button style="width:90px, height:40px">Enviar</button></center>
</form>
 1
Author: Leo Caracciolo, 2020-08-21 12:10:37

Good afternoon,

In the case is saving only one data, because the two fields are with the tag name= "text" changes the tag of the field password Ex: name= "password";

And change the php file to

   $texto = $_POST['texto'];
   $senha = $_POST['senha'];

If you want the visible password remove the password from the field and change it to text itself.

 0
Author: Jhoweb - Jonathan Gibim, 2020-08-20 19:41:16