0
I’m having a very common problem, and I don’t know how to solve it. I’m going to graze a very simple php page to explain.
The problem occurs when the user submits the form, and when he clicks on the back feature of the browser, the form is submitted again, generating a duplicity in the BD. You have some way of handling it?
Follow the code on my form:
<?php
//======================================================================================================================
// Envia formulário
//======================================================================================================================
if ((!empty($action)) and ($action === "add")) {
// Recebe dados do formulario
$nome = addslashes(filter_input(INPUT_POST, 'nome', FILTER_SANITIZE_SPECIAL_CHARS));
$idade = addslashes(filter_input(INPUT_POST, 'idade', FILTER_SANITIZE_NUMBER_INT));
// Verifica se informou os dados
if ((empty($nome)) || (empty($idade))) {
?>
<script>
alert('ERRO. Iforme todos os dados');
history.back();
</script>
<?php
die;
}
// Registra no BD
$inserir1 = Query($mysqli, "INSERT INTO cadastro VALUES ('0', '$nome', '$idade')");
// Verifica se cadastrou
if ($inserir1) {
?>
<script>
alert('ERRO. Iforme todos os dados');
window.location='cadastro.php';
</script>
<?php
} else {
?>
<script>
alert('ERRO. Tente mais tarde');
history.back();
</script>
<?php
}
}
//======================================================================================================================
?>
<form name='form' onsubmit='return validacao()' method=post action='cadastro.php?action=add' >
<input type="text" name="nome">
<input type="text" name="idade">
<input type="submit" name="finalizar">
</form>
when you save the data in the database, you can make a
reset
complete, redirecting with the functionheader
followed by aexit
. And you should also prepare the database so that it does not register replicas.– Edilson
When you check if you have registered in if Else only returns Alert with error? Can this Arnaldo?
– user60252