Update page when submitting template

Asked

Viewed 52 times

1

I have this code and form and when pressing register to insert in the table (which you already insert correctly) I wanted you to update the page automatically to update a query that I show before to the user.

<?php  

$servername = "xxx.xxx.x.xx";
$username = "xxxxx";
$password = "xxxxxxxx";
$dbname = "xxxxxxx"

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8');

$pedido = $_POST['NumPedido'];  
  $data = $_POST['DataSaida'];
$funcionario = $_POST['Funcionario'];
 $funcao = $_POST['Funcao'];   
 $IdTipoLuva = $_POST['IdTipoLuva'];
$tipoluva = $_POST['TipoLuva']; 
$IdTamanho = $_POST['IdTamanho'];  
 $tamanho = $_POST['Tamanho'];
$quantidade = $_POST['Quantidade']; 
 $produto = $_POST['Produto'];
 $qtd = $_POST['QtaHigiene'];
$observacoes = $_POST['Observacoes'];
$estado = $_POST['Estado'];  


$sql = "INSERT INTO RegSaidaLuvas (`NumPedido`,`DataSaida`,`Funcionario`,`Funcao`,`IdTipoLuva`,`TipoLuva`,`IdTamanho`,`Tamanho`,`Quantidade`,`Observacoes`) 
VALUES ('$pedido','$data','$funcionario','$funcao','$IdTipoLuva','$tipoluva','$IdTamanho','$tamanho','$quantidade','$observacoes')"; 

if ($conn->query($sql) === TRUE);

<form name="form" method="POST" onsubmit="return form_validation()" >

<h1><center><strong>Atribuição de Luvas</strong></center></h1></br>

<p><h5><strong>Número da Requisição</strong></h5> <input type="text" required="" id="NumPedido" name="NumPedido" /><br/></p>
<br/>
<label for=""><h5><strong>Nome Colaborador</strong></h5></label>
<select name="Colaborador">
       <option value="0">Selecione Colaborador</option>
        <?php
         $servername = "xxx.xxx.x.xx";
$username = "xxxxx";
$password = "xxxxxxxx";
$dbname = "xxxxxxx";

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8');
          
         $sql = "SELECT * FROM InfoLuvas WHERE Ativo = 1 ORDER BY Funcionario ASC";
         $qr = mysqli_query($conn, $sql);
         while($ln = mysqli_fetch_assoc($qr)){
            echo '<option value="'.$ln['Id'].'">'.$ln['Funcionario'].'</option>';
         }
      ?>        
    </select>
<br/>
<p><h5><strong>Data de Atribuição</strong></h5> <input type="date" required="" id="DataAtribuicao" name="DataAtribuicao" value="<?php echo date("Y-m-d");?>"/><br/></p>
<br/>
<p><h5><strong>Observações</strong></h5></br>
<textarea type="text" id="Observacoes" name="Observacoes" rows="2" cols="90"></textarea><br/></p>
<br/>
<p><h5><strong>Estado</strong></h5>
<p>&nbsp;&nbsp;&nbsp; <input type="radio" name="Estado" value="Entregue" required>Entregue 
<p><input type="submit" value="Registar"/>
</form>

  • INSERT is done after the information you want to show?

  • Yes, the query is right at the beginning of the page and only then comes the form

  • By the way, I don’t think it’s good practice to load the 2x page just to show up-to-date information, when you can do it once.

  • Thanks for the tip, it worked

2 answers

0

No form is included the action tag where will send the data via POST.

<form name="form" method="POST" action="arquivo.php" onsubmit="return form_validation()" >

Replace the.php file with the name of the file that will be sent the data.

And on the destination page, where you will receive the data, you point again to the document that is the form. As the example below:

header('Location: formulario.php');

Edited: In this case, only the function should be used header of PHP.

Replace this line:

if ($conn->query($sql) === TRUE);

For that reason:

if ($conn->query($sql) === TRUE){
  header('Refresh: 1; url=index.php');
}

Just replace the file name with the right one.

  • The action tag is not included, because it’s all done on the same page, so I don’t need to call any action.

  • @In this case, enter the remaining code of the page. So I’ll be able to better understand.

  • That way it doesn’t work

0


I believe it is a bad practice to have to load page 2x (give a refresh after the Submit) to display an updated data just entered in the database.

In your case, the problem is only structural. It is possible that the information is already updated as soon as the page is reloaded on Submit.

Change the structure of the code as follows:

<?php
// INSERT

// CONSULTA AO BANCO DE DADOS
?>
<form>
   FORMULÁRIO
</form>

In this way, with the INSERT before the query, the values will always be updated in the query when you make a Submit on the form.

Browser other questions tagged

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