0
Hello. I am creating a page of registration of people, every click on the save button is generated a csv file that the next page load should be used to show on the screen the already made entries, so that the user can know who is already registered and does not duplicate the process. I’m not getting the data saved in csv shown in their respective fields after the page is loaded. If anyone can help, thank you. The code I have so far is this:
<?php
//$csv = file_get_contents("/var/www/html/dados.csv");
//echo $csv;
//$multicasts = str_getcsv ($csv);
$meuArray = Array();
$file = fopen('dados.csv', 'r');
while (($line = fgetcsv($file)) !== false)
{
$meuArray[] = $line;
}
fclose($file);
print_r($meuArray);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cadastro</title>
<style type="text/css" media="all">
body{ font-family:Arial, Helvetica, sans-serif }
#tudo{ border:#CCCCCC 1px solid;width:550px;margin:0 auto }
.bd_titulo{
text-align:center;
background-color:#CCCCCC;
font-weight:bold
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/RobinHerbots/jquery.inputmask/3.x/dist/jquery.inputmask.bundle.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".telefone").inputmask({
mask: ["(99)99999-9999"],
keepStatic: true
});
});
$(function () {
$(".adicionarCampo").click(function () {
novoCampo = $("tr.linhas:first").clone();
novoCampo.find("input").val("");
novoCampo.insertAfter("tr.linhas:last");
$(".telefone").inputmask({
mask: ["(99)99999-9999"],
keepStatic: true
});
});
});
</script>
</head>
<body>
<form method="post" name="cadastro" action="">
<div id="tudo">
<table id = "multiTable" border="0" cellpadding="2" cellspacing="4">
<tr><td colspan="4" class="bd_titulo">Cadastro-Pessoas</td></tr>
<tr><td class="bd_titulo" width="10">Nome</td><td class="bd_titulo">Telefone</td><td class="bd_titulo">Função</td><td class="bd_titulo">Setor</td></tr>
<tr class="linhas">
<td><input type="text" name="nome[]" style="text-align:center" /></td>
<td><input type="text" name="telefone[]" class = "telefone"/></td>
<td>
<select name="funcao[]">
<option>Selecione</option>
<option value="vendedor" >vendedor</option>
<option value="gerente" >gerente</option>
<option value="assistente" >assistente</option>
</select>
</td>
<td><select name="setor[]">
<option>Selecione</option>
<option value="loja" >loja</option>
<option value="escritório" >escritório</option>
</select>
</td></td>
</tr>
<tr><td colspan="4">
<a href="#" class="adicionarCampo" title="Adicionar item"><img src="add.svg" border="0" /></a>
</td></tr>
<tr>
<td align="center" colspan="0">
<td align="right" colspan="4"><input type="submit" id="Salvar" value="Salvar" /></td>
</tr>
</table>
</form>
</div>
<?php
if ($_POST){
$nome = $_POST['nome'];
$telefone = $_POST['telefone'];
$funcao = $_POST['funcao'];
$setor = $_POST['setor'];
$quant_linhas = count($nome);
$dados = "";
for ($i=0; $i<$quant_linhas; $i++) {
$dados .= "$nome[$i],";
$dados .= "$telefone[$i],";
$dados .= "$funcao[$i],";
$dados .= "$setor[$i]";
$dados .= "\n";
}
$fileName = "dados.csv";
$fileHandle = fopen($fileName,"w");
fwrite($fileHandle,"$dados");
fclose($fileHandle);
$file = fopen($fileName, 'r');
fclose($file);
}
?>
</body>
</html>
Thanks for the help. So what you’ve given me is doing the same thing I’ve been able to echo in the csv file_get_contents. It plots what is saved in csv out of the corresponding inputs. I wanted him to plot in, like in the name have three saved lines, one written mary, another john and another ze and so on. I edited the post with an image for better viewing.
– Anne_M