1
I searched every corner of the Internet but I couldn’t find anything like my problem. I have a registration form that after doing the INSERT in the database shows a modal window and inside it a success or error message along with a "OK" button. This works perfectly by pressing OK it closes the modal and again displays the registration page. My problem is that if the user before pressing "OK" gives a "F5" is shown the dialog box "Confirm form forwarding" and if you click on "Continue" it duplicates the INSERT and consequently the registration in the database.
Follow the short code of PHP:
if (isset($_POST['cadastrar'])) {
<!-- aqui vão os requires dos includes com os dados do banco-->
$sql = "INSERT";
$resultado mysqli_query($conexao, $sql) or die (mysqli_error($conexao));
$linhas = mysqli_affected_rows($conexao);
if($linhas > 0){
$mensagem = "Seu cadastro foi feito com sucesso!";
} else {
$mensagem = "Erro ao cadastrar!";
}
<!-- aqui vai o require include de desconexão-->
<!--o código abaixo chama a janela modal-->
?><script>$(document).ready(function() {
$('#myModal').modal('show');
});</script>
<?php
}
?>
Below follows the modal window code:
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Mensagem:</h4>
</div>
<div class="modal-body">
<p><?php echo"$mensagem"; ?></p> <!--exibe a mensagem de erro ou sucesso-->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Below the scripts to fetch bootstrap and jquery commands:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <!--Banco de comandos para executar a janela modal-->
So basically what I would like is for you to press F5 during the display of the modal window, the connection with the bank to be closed and simply show the registration page and the modal to disappear. I tried to put a header ("Location:")
before the javascript that displays the modal, for it to redirect the page, but it redirects but does not display the modal. I’ve seen sites where this works, you register and is shown the modal, the F5 and the modal closes and does not request form forwarding.
All the code is in the same file, if you need any more data just ask. Thanks guys.
Your example looked very similar to sNniffer and I think it should work, but passing the variable $lines to the other file did not work the way you put it. Anyway it can help a lot of people. Thank you!
– Fernando Aguiar
You’re right! I edited the answer because I should have picked up the variable via GET. Now it’s ok.
– Pedro Souza