Save html form input in csv

Asked

Viewed 1,146 times

1

I created a form in html where the user type the name, email, phone, but I need to redeem these "entries" in csv for future firing email.

Can someone give me a hand on 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, they taught how to forward to a php file. My question is, form input can only go to php? If yes, you would have to create a function within php to feed the csv file?

I just graduated in html/css and Pyton, I don’t have much knowledge in php or other languages.

Thank you very much!!

2 answers

1

First you need to fix your HTML. Here are some changes I made to the form action, 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 it works the way it is in this code. This code will take the existing content from the.csv list and add a new line with the content of the form.

I hope I’ve solved your problem.

  • What is the need to load all the contents of the file in memory just to add a line to the end?

0

Yes, just as you possibly did in Python you will need to have PHP process your request and save the information in the file.

Possessing a array with the data you want to save in the CSV file you can use the function fputcsv. To do so, you must open the file in writing mode without overwriting the contents.

$data = ["foo", "bar", 1];

$handler = fopen("arquivo.csv", "a");

if (false !== $handler) {
    fputcsv($handler, $data);
    fclose($handler);
}

Other than another answer, that was based file_get_contents and file_put_contents, this form does not load the entire contents of the file in memory only to add a new line to the end, which is a major improvement in terms of performance and resource savings.

Considerations about the HTML

  1. Be consistent with the field names. Avoid naming a field starting with upper and lower case; this will facilitate maintenance.

  2. The guy type="mail" does not exist. The correct is type="email".

  3. Do not use <br> to set layout. See about this in <br> is obsolete?

  4. Do not use checkbox to ask for something that demands a single answer. Without getting into the gender merit, you can be just a man or woman, not both. Use radio or select in place.

  5. If you use radio the field name must be the same for all options. Equivalently, in checkbox the name must also be equal, followed by [] to indicate that multiple values will be possible.

  6. Do not use the attribute value of <input> to set a default message in the field, use placeholder instead.

Browser other questions tagged

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