To manipulate any information in the browser (without making a new request to the server) you should use JavaScript .
An example to remove the message after 5 seconds would be:
USING CLASS class='msg-success'
:
<!DOCTYPE html>
<html>
<body>
<p class='msg-success'> Usuário Cadastrado com sucesso!</p>
<script>
setTimeout(function(){
var msg = document.getElementsByClassName("msg-success");
while(msg.length > 0){
msg[0].parentNode.removeChild(msg[0]);
}
}, 5000);
</script>
</body>
</html>
USING ID id='msg-success'
:
<!DOCTYPE html>
<html>
<body>
<p id='msg-success'> Usuário Cadastrado com sucesso!</p>
<script>
setTimeout(function(){
var msg = document.getElementById("msg-success");
msg.parentNode.removeChild(msg);
}, 5000);
</script>
</body>
</html>
If you only use current browsers:
You can substibuir: msg.parentNode.removeChild(msg);
by simply: msg.remove();
Example with jQuery with ID: id='msg-success'
:
<!DOCTYPE html>
<html>
<body>
<p id='msg-success'> Usuário Cadastrado com sucesso!</p>
<script>
setTimeout(function(){
$('#msg-success').remove();
}, 5000);
</script>
</body>
</html>
Example with jQuery with CLASS: class='msg-success'
:
<!DOCTYPE html>
<html>
<body>
<p class='msg-success'> Usuário Cadastrado com sucesso!</p>
<script>
setTimeout(function(){
$('.msg-success').remove();
}, 5000);
</script>
</body>
</html>
Setting the time in the setTimeout function:
Defined in milliseconds, hence: 5000 / 1000 = 5 segundos.
Combining with PHP:
My suggestion: create a JavaScript function to remove the message and call this function every time the message appears.
If it is generated with PHP, it is generated along with page loading. So with javascript, an alternative is to create a function to remove the message after 5 seconds, then monitor when the page loads and soon after loading the page call the function.
As a result, after 5 seconds the message is displayed it will be removed.
JavaScript File:
function removeMensagem(){
setTimeout(function(){
var msg = document.getElementById("msg-success");
msg.parentNode.removeChild(msg);
}, 5000);
}
document.onreadystatechange = () => {
if (document.readyState === 'complete') {
// toda vez que a página carregar, vai limpar a mensagem (se houver)
// após 5 segundos
removeMensagem();
}
};
PHP File:
echo "<p id='msg-success'> Usuário Cadastrado com sucesso!</p>";