Save html form input in csv

0

I created a form in html where the user types the name, e-mail, phone, however I need to retrieve these "cadastral" records in csv for future e-mail firing.

Can anyone help me with how to feed this csv with inputs?

<form>  
  <input type="text" name="Nome" value="Nome"><br>
  <br>
  <input type="mail" name="mail" value="e-mail"><br>
  <br>
  Sexo<br>
  Masculino
  <input type="checkbox" name="homem" value="masculino"><br>
  Feminino
  <input type="checkbox" name="mulher" value="feminino"><br>
  <input type="submit" value="Submit">
</form>

All the articles I found, taught to pass to a php file. My question is, can one-way input to php? If so, would you have to create a function inside php to feed the csv file?

I just graduated in html / css and pyton, I do not have much knowledge in php or other languages.

Thank you very much !!

    
asked by anonymous 08.11.2018 / 15:37

1 answer

0

First you need to fix your HTML. Here are some changes I made to the action, form method, email field type, and how to handle checkboxes:

<form action="save.php" method="post">  
  <input type="text" name="nome" value="Nome"><br>
  <br>
  <input type="email" name="mail" value="E-mail"><br>
  <br>
  Sexo<br>
  Masculino
  <input type="checkbox" name="sexo[]" value="Masculino"><br>
  Feminino
  <input type="checkbox" name="sexo[]" value="Feminino"><br>
  <input type="submit" value="Submit">
</form>

And in the save.php file:

<?php
    //Caminho e nome do arquivo (se colocar só o nome do arquivo, ele deve estar na mesma pasta do PHP)
    $file = "lista.csv";
    //Carregar o arquivo existente
    $current = file_get_contents($file);
    //Criar (usando informações fornecidas pelo formulário HTML) e adicionar nova linha ao conteúdo já existente
    $current .= $_POST['nome'].', '.$_POST['mail'].', '.$_POST['sexo'][0]."\n";
    //Adicionar conteúdo todo ao arquivo
    file_put_contents($file, $current);
?>

Create the .csv list file in the same folder as this HTML and PHP so that it works the way it is in that code. This code will grab the existing content from the .csv list and add a new line to the contents of the form.

I hope I have solved your problem.

    
09.11.2018 / 01:25