To manipulate any information in the browser (without making a new request to the server) you must 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>
Only current browsers are used:
Can substitute: msg.parentNode.removeChild(msg);
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>
Time setting in setTimeout function:
Set in milliseconds, so: 5000 / 1000 = 5 segundos.
Combining with PHP:
My suggestion: create a Javascript function to remove the message and call this function each time the message appears.
If it is generated with PHP, it is generated along with page loading. Then with javascript, an alternative is to create a function to remove the message after 5 seconds, then monitor when the page is loaded and right after the page load 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>";
Javascript might be better to do this, since in php, you would need to update the page again.
– user28595
I thought about it tbm, but I would need a way to capture the moment the message appears on the screen with Javascript, in order to destroy it at some point. I have no idea how to do it.
– user42676
Perhaps delegating the display and removal of the message using javascript, through jquery. It would be interesting for you to add the javascript and/or jquery tag if you accept answers based on these technologies.
– user28595
True, I added. I’m going to do a search on a way to replace echo.
– user42676
Put what you have in javascript done.
– Papa Charlie
There was no code yet, I was trying to PHP before I left pro JS. But I will do like the answers below.
– user42676