<?php
session_start();
if(isset($_POST["button"])) {
$email = $_POST["email"];
$lozinka = $_POST["lozinka"];
$korisnik = new Korisnik(1, "", "", $email, $lozinka);
$odgovor = Korisnik::prijava($email, $konekcija);
$rez = $odgovor->fetch_array(MYSQLI_ASSOC);
$lozinka = $rez["lozinka"] ?? " ";
$korisnikID = $rez["korisnikID"] ?? " ";
$greska = 0;
if (!password_verify($_POST["lozinka"], $lozinka)) {
$greska = 1;
} else {
$_SESSION["korisnik"] = $korisnikID;
setcookie("korisnik", $korisnikID, time() + 60*60*60);
header("Location: glavna.php");
exit();
}
}
?>
[...]
<body>
<form class="container" method="post" action="" id="forma" enctype="multipart/form-data" novalidate>
<input type="email" name="email" id="email" class="input" placeholder="Email" required>
<input type="password" name="lozinka" id="lozinka" class="input" placeholder="Šifra" required>
<div id="nepoklapase" <?php if ($greska == 1) {?> style="display:flex" <?php } else { ?> style = "display:none" <?php } ?> >
<h5 class="crvena">Podaci se ne poklapaju</h5>
</div>
<button class="button" name="button" id="button" type="submit">Prijavi se</button>
</form>
</div>
<script>
[getDocumentById code]
form
.addEventListener(
'submit',
(event) => {
email.classList.toggle('error', !email.validity.valid);
lozinka.classList.toggle('error', !lozinka.validity.valid);
faliemail.classList.toggle('promeni', !email.validity.valid);
falilozinka.classList.toggle('promeni', !lozinka.validity.valid);
if(!email.validity.valid){
event.preventDefault();
}
if(!lozinka.validity.valid){
event.preventDefault();
}
<?php if ($greska == 1) { ?>
event.preventDefault();
<?php
$greska == 0;
} ?>
}
);
[...]
</script>
I’m trying to set the value of greska
to 1 when the email and password of the user don’t match so that the nepoklapase
div becomes displayed with the proper message and the form stops from submitting. With the code I have the form keeps submitting and the message stays displayed.
I sort of get what the problem with displaying the message is, the value doesn’t reset and I think I can solve that later, but I can’t get the form to stop submitting. How do I do that?