<?php
// Start the session
session_start();
// Check if the 'userid' session is not set
if (!isset($_SESSION['userid'])) {
header("Location: ./login.php");
exit();
}
// Include database connection and other necessary files
include_once ('_db.php');
// Set the user ID from the session variable
$userid = $_SESSION['userid'];
// Check if form data is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve user ID
$userid = $_POST['userid'];
// Retrieve withdrawal amount
$withdrawalAmount = $_POST['amount'];
// Query to retrieve user's account balance
$stmt = $conn->prepare("SELECT account_balance FROM user_login WHERE userid = ?");
$stmt->bind_param("i", $userid);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
// Check if withdrawal amount is greater than account balance
if ($withdrawalAmount > $row['account_balance']) {
// Insufficient balance, display error message
echo "Insufficient balance. Please enter an amount less than or equal to your account balance.";
} else {
// Sufficient balance, proceed with withdrawal process
// Add your withdrawal process code here
}
}
// Include header and topbar
include_once("include/header.php");
include_once("include/topbar.php");
// Include search bar
include_once("search.php");
?>
<div class="container">
<h1>Withdraw Funds</h1>
<!-- Deposit form -->
<div class="card">
<div class="card-body">
<?php
session_start();
// Check if withdrawal success message is set
if (isset($_SESSION['withdrawal_success'])) {
echo '<div class="alert alert-success" role="alert" style="background-color: #d4edda; border-color: #c3e6cb; color: #155724;">';
echo $_SESSION['withdrawal_success'];
echo '</div>';
// Unset the session variable to avoid displaying the message again on refresh
unset($_SESSION['withdrawal_success']);
}
// Check if withdrawal error message is set
if (isset($_SESSION['withdrawal_error'])) {
echo '<div class="alert alert-danger" role="alert" style="background-color: #f8d7da; border-color: #f5c6cb; color: #721c24;">';
echo $_SESSION['withdrawal_error'];
echo '</div>';
// Unset the session variable to avoid displaying the message again on refresh
unset($_SESSION['withdrawal_error']);
}
?>
<?php
// Assume $account_balance is the balance of the logged-in user retrieved from the database
$account_balance = 100.00; // Example balance
// Check if the user has funds available
if ($account_balance > 0) {
// User has funds available, display the withdrawal form with the input field for entering the withdrawal amount
?>
<form method="post" action="withdrawal_process" class="payOut">
<input type="hidden" name="userid" value="<?php echo $_SESSION['userid']; ?>">
<div class="withBl">
<div class="row">
<!-- <h2 class="capTitleIn">Make A Withdrawal</h2> -->
<div class="col-md-6">
<div class="col">
<div class="inputLine">
<label for="calc_amount">WALLET ADDRESS:</label>
<input type="text" class="form-control" id="proof_of_payment" name="wallet_address">
</div>
</div>
</div>
<div class="col-md-6">
<div class="col">
<div class="inputLine">
<label for="withdrawal_amount">Amount:</label>
<input type="text" name="amount" value="0.00" id="withdrawal_amount" class="form-control">
<span class="cur">$</span>
</div>
</div>
</div>
<div class="bot">
<button type="submit" id="withdraw_button" class="btn btn-success">Withdraw</button>
</div>
</div>
</div>
</form>
<script>
// Function to check if withdrawal amount exceeds account balance and enable/disable Withdraw button
function validateWithdrawalAmount() {
var withdrawalAmount = parseFloat(document.getElementById('withdrawal_amount').value);
var accountBalance = <?php echo $row['account_balance']; ?>; // PHP variable containing account balance
var withdrawButton = document.getElementById('withdraw_button');
// Check if withdrawal amount is greater than account balance
if (withdrawalAmount > accountBalance) {
document.getElementById('balance_error').innerHTML = "Insufficient balance. Please enter an amount less than or equal to your account balance.";
withdrawButton.disabled = true; // Disable the button
} else {
document.getElementById('balance_error').innerHTML = ""; // Clear error message
withdrawButton.disabled = false; // Enable the button
}
}
// Attach event listener to withdrawal amount input field
document.getElementById('withdrawal_amount').addEventListener('input', validateWithdrawalAmount);
</script>
<?php
} else {
// User has no funds available, display a message
?>
<div class="right">
<h2 class="capTitleIn">Enter amount</h2>
<br><br>
You have no funds to withdraw.
</div>
<?php
}
?>
</div>
</div>
</div>
<?php include_once("include/footer.php"); ?>