Clear Form after Submit and continue on the same page, only with PHP and Html

Asked

Viewed 1,218 times

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.

  • 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());

  • 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

  • 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.

  • Since your registration page is.php registration, you should not use header("Location: cadastro.php") ?

  • Thiaguinho then some idea of why mine doesn’t work?

  • Puis here register.php for a better understanding of the correct file name, pgcadpro.php

  • 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.

  • I believe that his header is not working because you put it inside the TAG script, 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 the Location defined.

Show 4 more comments

1 answer

2


Do not need to redirect, because, you are referring to your previous question where I suggested using an iframe to save the data, and the page will be redirected within the hidden iframe, which will not change the main page.

In this case you can do it in two ways:

Emptying the field:

parent.document.getElementById("txtcategoria").value = '';

Or resetting the form:

parent.document.getElementById("form-cat").reset();

You can also focus on the field after resetting the form:

parent.document.getElementById("txtcategoria").focus();
  • The tension is that he said he would not like to use Javascript.

  • @Thiaguinhoembasamento but he is already using. Take a look at <script>. I think he expressed himself wrong when he says "only with PHP + HTML", and Javascript is native in the browser.

  • All right, Sam.

  • @Thank you very much, that’s what I needed. Just so I understand Parent.document.getElementById("form-cat"). reset(); this is Javascript??

  • Is Javascript yes.

  • Sam, I’m blocked from asking questions but I only have a percussion with -2 because this as duplicate, I can not exclude because it says that people have invested time with her and helped some... what to do?

Show 1 more comment

Browser other questions tagged

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