Could someone tell me how to make disappear a message after a few seconds?
$msg = '<h4 class="col-12 text-center mb-3 text-success">Sikeres rögzítés!</h4>';
After receiving the POST, it stays there and I need to reload to make it disappear.
Thanks for the help.
include 'settings.php';
$msg = '';
if(isset($_POST['rogzit'])) {
if(isset($_POST['vonalkod']) && isset($_POST['garnitura'])) {
$vonalkod = $_POST['vonalkod'];
$garnitura = $_POST['garnitura'];
if(strlen($vonalkod) > 10) {
$msg = '<h4 >Nem megfelelő a vonalkód beolvasása!</h4>';
} else if(strlen($vonalkod) < 10) {
$msg = '<h4 >Nem megfelelő a vonalkód beolvasása!</h4>';
} else if(strlen($garnitura) < 0) {
$msg = '<h4 >Nincs garnitúra beírva!</h4>';
} else {
$sql_beszuras = "INSERT INTO gsaa(vonalkod, garnitura) VALUES ('$vonalkod', '$garnitura')";
if(mysqli_query($conn, $sql_beszuras)) {
$msg = '<h4 >Sikeres rögzítés!</h4>';
} else {
$msg = '<h4 >Sikertelen rögzítés!</h4>';
}
}
} else {
$msg = '<h4 >Hiányzó adat a POST kérésből!</h4>';
}
}
?>
CodePudding user response:
This has to be done client-side (i.e. in javascript) since PHP has finished after echoing the response (and, PHP only works server-side).
Here's a suggested algorithm (UNTESTED pseudo-code):
On the PHP side, add the class autovanish
(or whatever you want) to the element you want to automatically vanish after so many seconds.
$msg = '<h4 class="autovanish col-12 text-center mb-3 text-success">Sikeres rögzítés!</h4>';
Then, have a javascript function running all the time:
function autovanish(){
const avDivs = document.getElementsByClassName('autovanish');
if (avDivs.length){
setTimeout(() => {
avDivs[0].remove();
}, 3000); //removes the element after 3000ms
}
setTimeout(() => {autovanish();}, 500); //re-run every 500ms
}
In case you are not a js guy, you need the [0]
after avDivs
because getElementsByClassName
returns a collection, not a string.
If you need more assistance than this pseudo-code, ask in the comments.
CodePudding user response:
// Adatbázis csatlakozás
require_once 'php/database.php';
// Bejelentkezés vizsgálat
require_once 'php/logincheck.php';
// HTML szerkezet eleje
$title = 'Főoldal';
require_once 'components/htmltop.php';
// Navigációs sáv
require_once 'components/navbar.php';
$msg = '';
if(isset($_POST['rogzit_in'])) {
if(isset($_POST['barcode'])) {
$barcode = $_POST['barcode'];
if(strlen($barcode) < 0) {
$msg = '<h4 class="col-12 text-center mb-3 text-danger">Nem megfelelő a vonalkód beolvasása!</h4>';
} else {
$sql_beszuras = "INSERT INTO log_in(barcode) VALUES ('$barcode')";
if(mysqli_query($dbCon, $sql_beszuras)) {
$msg = '<h4 class="autovanish col-12 text-center mb-3 text-success">Sikeres rögzítés!</h4>';
} else {
$msg = '<h4 class="autovanish col-12 text-center mb-3 text-danger">Sikertelen rögzítés!</h4>';
}
}
} else {
$msg = '<h4 class="autovanish col-12 text-center mb-3 text-danger">Hiányzó adat a POST kérésből!</h4>';
}
}
if(isset($_POST['rogzit_out'])) {
if(isset($_POST['barcode'])) {
$barcode = $_POST['barcode'];
if(strlen($barcode) < 0) {
$msg = '<h4 class="col-12 text-center mb-3 text-danger">Nem megfelelő a vonalkód beolvasása!</h4>';
} else {
$sql_beszuras = "INSERT INTO log_out(barcode) VALUES ('$barcode')";
if(mysqli_query($dbCon, $sql_beszuras)) {
$msg = '<h4 class="autovanish col-12 text-center mb-3 text-success">Sikeres rögzítés!</h4>';
} else {
$msg = '<h4 class="autovanish col-12 text-center mb-3 text-danger">Sikertelen rögzítés!</h4>';
}
}
} else {
$msg = '<h4 class="autovanish col-12 text-center mb-3 text-danger">Hiányzó adat a POST kérésből!</h4>';
}
}
?>
<!DOCTYPE html>
<html lang="hu">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Ubuntu:wght@400;500;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">
<link rel="stylesheet" href="css/style.css" media="screen">
<title><?=$title?> - KARSOL</title>
</head>
<body>
<div class="col-6 mx-auto">
<form action="" method="post" class="col-12 mx-auto mb-3 form-group bg-white shadow p-3 text-center">
<h4 class="col-12 text-center my-3">Gyártás rögzítés</h4>
<label>Vonalkód:</label>
<input type="text" name="barcode" class="form-control mb-3 col-6 mx-auto" required autocomplete="off">
<button type="submit_in" name="rogzit_in" class="btn btn-dark">Belépés</button>
<button type="submit_out" name="rogzit_out" class="btn btn-dark">Kilépés</button>
<?=$msg?>
</form>
</div>
<script>
document.addEventListener('DOMContentLoaded',() => {
autovanish();
}
function autovanish(){
const avDivs = document.getElementsByClassName('autovanish');
if (avDivs.length){
setTimeout(function(){
avDivs[0].remove();
}, 3000); //removes the element after 3000ms
}
setTimeout(() => {autovanish();}, 500); //re-run every 500ms
}
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg OMhuP IlRH9sENBO0LRn5q 8nbTov4 1p" crossorigin="anonymous"></script>
</body>
</html>```