0
Hello,
I’ve been having a doubt for two days, I’m trying everything and still can’t, if anyone can help me!!
I made a form in HTML and PHP, where I fill out the form and after giving Submit i Gero a file . csv with the data filled, so far so good.. The problem itself, is that I made a JS script to clone the form fields, to submit more than 1 request, and I’m not able to take the values of the forms that have been cloned and play inside the csv file.
Follows the code..
<?php
$error = '';
$projeto = '';
$quantidade = '';
$altura = '';
$largura = '';
$material = '';
$descricao = '';
function clean_text($string)
{
$string = trim($string);
$string = stripslashes($string);
$string = htmlspecialchars($string);
return $string;
}
if(isset($_POST["submit"]))
{
if(empty($_POST["projeto"]))
{
$error .= '<p><label class="text-danger">Descreva o Projeto</label></p>';
}
else
{
$projeto = clean_text($_POST["projeto"]);
}
if(empty($_POST["quantidade"]))
{
$error .= '<p><label class="text-danger">Informe a Quantidade</label></p>';
}
else
{
$quantidade = clean_text($_POST["quantidade"][0]);
if(!preg_match("/^[0-9 ]*$/",$quantidade))
{
$error .= '<p><label class="text-danger">É Permitido Apenas Números e Espaços</label></p>';
}
}
if(empty($_POST["altura"]))
{
$error .= '<p><label class="text-danger">Informe a Altura</label></p>';
}
else
{
$altura = clean_text($_POST["altura"][0]);
if(!preg_match("/^[0-9 (,)]*$/",$altura))
{
$error .= '<p><label class="text-danger">É Permitido Apenas Números e Espaços</label></p>';
}
}
if(empty($_POST["largura"]))
{
$error .= '<p><label class="text-danger">Informe a Largura</label></p>';
}
else
{
$largura = clean_text($_POST["largura"][0]);
if(!preg_match("/^[0-9 (,)]*$/",$largura))
{
$error .= '<p><label class="text-danger">É Permitido Apenas Números e Espaços</label></p>';
}
}
if(empty($_POST["material"]))
{
$error .= '<p><label class="text-danger">Selecione o Material</label></p>';
}
else
{
$material = clean_text($_POST["material"][0]);
}
if(empty($_POST["descricao"]))
{
$error .= '<p><label class="text-danger">Informe a Descrição</label></p>';
}
else
{
$descricao = clean_text($_POST["descricao"][0]);
}
if($error == '')
{
$form_data = array(
"$projeto\n",
"\n \nQuantidade; Largura; Altura; Material; Descricao\n $quantidade[0]; $largura[0]; $altura[0]; $material[0]; $descricao[0]\n"
);
$file_open = fopen("$quantidade;".date("d-m-y;h-i-s").".csv", "a");
print_r($_POST);
fputcsv($file_open, $form_data);
$error = '<label class="text-success">Pedido Enviado com Sucesso!</label>';
$projeto = '';
$quantidade = '';
$altura = '';
$largura = '';
$material = '';
$descricao = '';
}
//header('Location: /planilha/formulario.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Teste CSV FILE</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var elm_html = $('#clone-form').html();
$(document).on('click', '.clone', function(e){
e.preventDefault();
var i = $('.cadastroPecas').length;
var elementos = elm_html.replace(/\[[0\]]\]/g, '['+i+++']');
$('#clone-form').append(elementos);
});
$("#clone-form").on('click', '.delete-clone', function() {
$(this).parent().remove();
});
});
</script>
<nav class="navbar navbar-dark bg-primary">
<!-- Navbar content -->
</nav>
</head>
<body>
<br />
<div id="conteudo" class="container">
<h3 align="center">Peças a Serem Cortadas</h3>
<br />
<?php echo $error; ?>
<hr>
<div class="row">
<form method="post">
<div class="form-group col-md-14">
<input type="text" name="projeto" class="form-control" placeholder="Descrição do Projeto:" />
</div>
<div class="row engloba" id="clone-form">
<div>
<div class="form-group col-md-2">
<input type="text" name="quantidade[]" class="form-control" placeholder="Quantidade:" />
</div>
<div class="form-group col-md-2">
<input type="text" name="largura[]" class="form-control" placeholder="Largura:" />
</div>
<div class="form-group col-md-2">
<input type="text" name="altura[]" class="form-control" placeholder="Altura:" />
</div>
<div class="form-group col-md-2">
<select name="material[]" class="form-control">
<option value="">Material:</option>
<option value="1 - MDF Branco 18mm">1 - MDF Branco 18mm</option>
<option value="2 - MDF Wengue 15mm">2 - MDF Wengue 15mm</option>
<option value="3 - MDF Carvalho 18mm">3 - MDF Carvalho 18mm</option>
</select>
</div>
<div class="form-group col-md-3">
<input type="text" name="descricao[]" class="form-control" placeholder="Descrição:" />
</div>
<button type="button" class="clone btn btn-outlined btn-success">+</button>
<button type="button" class="delete-clone btn btn-outlined btn-danger">-</button>
<hr>
</div>
</div>
<div class="row">
<div class="form-group" align="center">
<button type="submit" name="submit" class="btn btn-info">Calcular</button>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
when you give the Submit, the fields with
name="valor[]"
are passed off asarray()
to thePHP
, would, for example,$_POST['descricao']
as aarray()
, you would have to separate by keys, because each key matches your respective<input>
– Wees Smith