Whenever I refresh the page the modal automatically opens even though I don't press the button. Is there a way in my if statement to make it only open when the button is pressed? Thanks!
I am very new to PHP so my code may not be efficient.
<?php
include_once 'includes/database.php';
if ($_SESSION['renterID'] == 0){
$sql = "SELECT firstName, lastName, paymentID, paymentAmount, paymentDate, paymentPaid FROM renter, payment WHERE renter.renterID = payment.renterID ORDER BY paymentDate ASC";
} else {
$sql = "SELECT firstName, lastName, paymentID, paymentAmount, paymentDate, paymentPaid FROM renter, payment WHERE renter.renterID = payment.renterID AND $_SESSION[renterID] = renter.renterID";
}
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while($row = $result->fetch_assoc()) {
echo
"<tr>
<td>$row[paymentID]</td>
<td>$row[firstName] $row[lastName]</td>
<td>$row[paymentAmount]</td>
<td>";echo date ('F d, Y', strtotime($row['paymentDate'])); echo "</td>";
if ($_SESSION['renterID'] == 0) {
if ($row['paymentPaid'] == '0') {
echo "<td class='text-center'><a class='btn btn-danger btn-sm'><i class='fa fa-times' aria-hidden='true'>Paid</i></a></td>";
} else {
echo "<td class='text-center'><a class='btn btn-success btn-sm'><i class='fa fa-check' aria-hidden='true'>Paid</i></a></td>";
}
} else {
if ($row['paymentPaid'] == '0'){
echo "
<td class='text-center'><button href='#' class='btn btn-success btn-sm' id='payNowButton' name='payNowID' value='$row[paymentAmount]'><i class='fa fa-usd' aria-hidden='true'>Pay Now</i></button></td>
";
} else {
echo "<td class='text-center'><a class='btn btn-sm'><i class='fa fa-check' aria-hidden='true'>Paid</i></a></td>";
}
}
echo "</tr>";
}
} else {
echo "<tr><td colspan='5'>No payments at this time</td></tr>";
}
<?php
if(isset($_POST['payNowID'])){
$totalAmount = $_POST['payNowID'];
echo "<script>
$(document).ready(function() {
$('#payNowButton').click(function() {
$('#pay_now').modal('show');
});
});
</script>";
}
?>