0
I created a dialog to register "Categories" and "Products" in the database, everything is working correctly, but I would like that when I click on the Save "Submit" button it clears the campus inputs after saving in the database.
I have tried several ways found both in this and in other forums, but most solutions are via ajax that I have no knowledge yet, if anyone can help me via PHP + HTML I thank you already.
Gravacategoria.php:
<?php
$nomecat=$_POST["txtcategoria"];
if(!empty($nomecat)){
$conn= new PDO("mysql:host=localhost:3306;dbname=loja","root","aula");
$stmt = $conn->prepare('Insert into loja.categoria(nome)Values(:par_categoria)');
$dados=array(":par_categoria"=>$nomecat);
$result = $stmt->execute($dados);
if($result){
$mensagem = "Produto inserido com sucesso";
}
else{
$mensagem = "Erro";
}
?>
<script>
parent.document.getElementById("msgaqui").innerHTML = "<?php echo $mensagem ?>";
// MINHA ULTIMA TENTATIVA, CHAMANDO A PAGINA DE CADASTRO DE NOVO, POREM TAMB EM NAO FUNCIONOU, OS CAMPOS CONTINUAM PREENCHIDO
header("Location: pgcadpro.php");
</script>
<?php
}
?>
Cadastro.php:
<form id="form-cat" target="gravar" method="post" action="gravacategoria.php">
<div class="container">
<div class="form-group">
<label class="col-md-4 control-label" for="txtproduto">Nova Categoria: </label>
<div class="col-md-6">
<input id="txtcategoria" name="txtcategoria" type="text" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="btnsalvar"></label>
<div class="col-md-8">
<button id="btnsalvarcat" name="btnsalvar" type="Submit" class="btn btn-primary">Salvar</button>
<button type="reset" id="btncancelar" name="btncancelar" class="btn btn-danger">Cancelar</button>
</div>
<div class="col-md-4">
<div class="alert alert-success" role="alert" id="msgaqui"> </div>
</div>
</div>
</div>
</form>
Thank you!
Unfortunately you will need Javascript for this, either with AJAX or jQuery, only with PHP and HTML you will not be able to do, being because HTML is just its structure and cannot perform " actions " after some event and PHP only interferes on the server side. The Javascript that will make this client-side feature.
– Leo
I think the Leo solution is the most suitable one, however you can try to "trick" the browser by calling the sign-up page again, but adding some parameter to the url, assigning a dynamic value, for example: header('Location: pgcadpro,php? clean=' . time());
– Thiago Schettini
Thanks friend, however I need to deliver today and so far whenever I needed a solution to get around my problem without ajax was here I managed, I know the correct is to study Javascript however I do not know how it works and I do not have time today to learn, I’ll just hope someone shows up with a snipe. Thank you
– Marcelo Augusto Colombo
I didn’t understand the problem of not being able to use the header('Location') ... When I use the header going back to the registration page, the fields always clear.
– Gato de Schrödinger
Since your registration page is.php registration, you should not use header("Location: cadastro.php") ?
– Gato de Schrödinger
Thiaguinho then some idea of why mine doesn’t work?
– Marcelo Augusto Colombo
Puis here register.php for a better understanding of the correct file name, pgcadpro.php
– Marcelo Augusto Colombo
I can suggest to you a heavy gambit that I made a made. You can use the header("Location") by directing to a send confirmation page with and containing a message like "Your Registration Has Been Successfully" and on this page you create a button directing again to the registration page. Then, when the user clicks on this button, it goes back to the registration page.
– Gato de Schrödinger
I believe that his
header
is not working because you put it inside the TAGscript
, it is recommended to always leave it at the end of your PHP script, as soon as all the code is completed, it will redirect the user to theLocation
defined.– Leo