Save form output to txt

Asked

Viewed 266 times

0

I’m trying to save what is written in a form in a txt file while the site’s Mysql does not update. I’ve seen an answer here, but it doesn’t fully answer my question

What can I use to save the form information to a text file after clicking the button? remembering that I want to use the <button id="btn-enviar"... and not a Ubmit.

<div class="row">
        <form id="form-enviar" name="nome" class="col s12" method="post">
            <input type="hidden" id="act" name="act" value="ENVIAR">
            <input type="hidden" id="private" name="private" value="">
            <div class="row">
                <div class="input-field col s12">
                    <input name="nome" id="nome" type="text" maxlength="40">
                    <label for="nome" class="active">Nome completo</label>
                </div>
            </div>
            <div class="row">
                <div class="input-field col s12">
                    <input name="idade" id="idade" type="text" maxlength="40">
                    <label for="idade" class="active">idade</label>
                </div>
            </div>
        </form>
        <div class="row">
                <div class="col s12 center-align">
                   <a href="javascript:void(0);" class="btn-large modal-action modal-close waves-effect waves-purple  btn-flat">CANCELAR</a>
                  <button id="btn-enviar" type="submit" class="waves-effect waves-light btn-large purple darken-1">Enviar</button>

                </div>
        </div>         
    </div>
</div>
  • Good night, use PHP’s file_put_content function: https://www.php.net/manual/en/function.file-put-contents.php

1 answer

0

You can do this using the php fopen function and to take the data from the Voce form you will need a request of type Submit (get or post) it is not possible to take the data with php without submitting a request with Submit get or post as follows

<form method="get" id="form-example">

 <input type="text" name="campoExemplo">
 <button type="submit">Enviar</button>

</form>

<?php

     //Captura os dados do formulario
     $campExemplo =  $_GET['campoExemplo'];

     $name = 'arquivo.txt';

     //Concatena os valores no formato que quer salvar Exemplo: csv
     $text = $campExemplo . ";". "";

     //Abre o arquivo para gravar se não existir ele cria
     $file = fopen($name, 'a');  

     fwrite($file, $text); // Grava os dados no arquivo
     fclose($file); // Fecha o arquivo que foi aberto


      ?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.