I am making a simple PHP login with POST. However, every time I load the page, the function gets fired before I even hit submit on the html form. I have tried the method below and isset($_POST['submit']) but both ways are firing every single time I load the page. How do I stop this from occuring? Thank you.
//PHP CODE ABOVE HTML
<?php
$serverName = "localhost";
$dBUserame = "blake";
$dBPassword = "password";
$dBName = "database";
session_start();
$conn = mysqli_connect($serverName, $dBUserame, $dBPassword, $dBName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = mysqli_real_escape_string($conn, $_POST['email']);
$password = mysqli_real_escape_string($conn ,$_POST['password']);
$sql = "SELECT * FROM users WHERE email = '$email' AND password = '$password'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$_SESSION['useremail'] = $email;
header('Location: https://allevohealth.com/admin/newsletter/contacts.php');
// output data of each row
while ($row = $result->fetch_assoc()) {
$_SESSION['name'] = $row["fullname"];
$_SESSION['email'] = $row["email"];
$_SESSION['useridid'] = $row["id"];
echo "<br> Full Name: ". $row["fullname"]. " - Email: ". $row["email"]. " " . $row["passsword"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
}
?>
//HTML CODE BELOW PHP
<div class="col-lg-12 row justify-content-center">
<div class="col-lg-4 col-md-8 col-sm-9 mb-5">
<div class="p-5" style="border-radius: 20px !important; background-color: #161621">
<form id="myform" method="post">
<h2 class="h4 text-white mb-5">ACCOUNT LOGIN</h2>
<div class="row form-group">
<div class="col-md-9">
<label class="text-white" for="email">EMAIL</label>
<input type="email" id="input-email" name="email" class="form-control rounded-0">
<p id="result" style="padding-top: 2%; font-weight: bold;"></p>
</div>
</div>
<div class="row form-group">
<div class="col-md-7 mb-3 mb-md-0">
<label class="text-white" for="password">PASSWORD</label>
<input type="password" name="password" id="input-password" class="form-control rounded-0">
</div>
</div>
<div class="row form-group">
<div class="col-md-12" style="padding-top: 5%">
</div>
</div>
<input type="submit" name="submit" value="LOGIN" style="width: 150px" class="btn btn-primary mr-4 mb-2">
</form>
</div>
</div>
</div>
";`, at various points in the script (before the if statement, inside if of it in random spots), what do you get? – aynber Jul 07 '22 at 17:15