Refresh page while submitting form

1

I have this code and form and clicking on register to insert into the table (which already inserts correctly) I wanted to update the page automatically to update a query that I show the user before.

<?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>
    
asked by anonymous 19.01.2018 / 15:12

2 answers

0

I believe it is a bad practice to have to load page 2x (give a refresh after submit ) to display an updated data that has just been inserted into the database .

In your case, the problem is just structural. It is possible that information may already be updated as soon as the page is reloaded in submit .

Change the code structure as follows:

<?php
// INSERT

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

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

    
19.01.2018 / 19:01
0

No form is included in the action tag where it 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 landing page, where you will receive the data, you again point to the document that is in the form. Like the example below:

header('Location: formulario.php');

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

Replace this line:

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

For this:

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

Just replace the file name.

    
19.01.2018 / 15:52