You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
215 lines
7.2 KiB
215 lines
7.2 KiB
6 months ago
|
<?php
|
||
|
require 'db_connection.php';
|
||
|
session_start();
|
||
|
|
||
|
// Check if the user is logged in
|
||
|
if (!isset($_SESSION['user_email'])) {
|
||
|
header("Location: login.php");
|
||
|
exit;
|
||
|
}
|
||
|
|
||
|
// Retrieve logged-in user details
|
||
|
$user_email = $_SESSION['user_email'];
|
||
|
$stmt = $conn->prepare("SELECT id, first_name, last_name FROM users WHERE email = ?");
|
||
|
$stmt->bind_param("s", $user_email);
|
||
|
$stmt->execute();
|
||
|
$result = $stmt->get_result();
|
||
|
$user = $result->fetch_assoc();
|
||
|
$user_id = $user['id'];
|
||
|
$stmt->close();
|
||
|
|
||
|
// Handle form submission
|
||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
|
$item_name = $_POST['item_name'];
|
||
|
$condition = $_POST['condition'];
|
||
|
$quantity = $_POST['quantity'];
|
||
|
$latitude = $_POST['latitude'];
|
||
|
$longitude = $_POST['longitude'];
|
||
|
|
||
|
// Validate inputs
|
||
|
if (empty($item_name) || empty($condition) || empty($quantity) || empty($latitude) || empty($longitude)) {
|
||
|
$error_message = "All fields are required, including location.";
|
||
|
} else {
|
||
|
// Insert donation into the database
|
||
|
$stmt = $conn->prepare("INSERT INTO material_donations (donor_id, item_name, conditions, quantity, latitude, longitude, status) VALUES (?, ?, ?, ?, ?, ?, 'pending')");
|
||
|
|
||
|
if ($stmt) {
|
||
|
$stmt->bind_param("issidd", $user_id, $item_name, $condition, $quantity, $latitude, $longitude);
|
||
|
$execute = $stmt->execute();
|
||
|
|
||
|
if ($execute) {
|
||
|
$success_message = "Your donation has been submitted successfully and is pending approval.";
|
||
|
header("Location: dashboard.php"); // Redirect to the dashboard after successful submission
|
||
|
exit;
|
||
|
} else {
|
||
|
$error_message = "Failed to submit your donation. Please try again. Error: " . $stmt->error;
|
||
|
}
|
||
|
} else {
|
||
|
$error_message = "Failed to prepare SQL query. Please check your database connection.";
|
||
|
}
|
||
|
|
||
|
$stmt->close();
|
||
|
}
|
||
|
}
|
||
|
?>
|
||
|
|
||
|
<!DOCTYPE html>
|
||
|
<html lang="en">
|
||
|
|
||
|
<head>
|
||
|
<meta charset="UTF-8">
|
||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
<title>Donate Materials</title>
|
||
|
<!-- Link to Bootstrap 5 CSS for a clean design -->
|
||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
|
||
|
<!-- Leaflet CSS for map -->
|
||
|
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
|
||
|
<style>
|
||
|
body {
|
||
|
background-color: #f8f9fa;
|
||
|
}
|
||
|
|
||
|
.container {
|
||
|
margin-top: 30px;
|
||
|
padding: 30px;
|
||
|
background-color: white;
|
||
|
border-radius: 8px;
|
||
|
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
||
|
}
|
||
|
|
||
|
.map-container {
|
||
|
height: 400px;
|
||
|
width: 100%;
|
||
|
border-radius: 8px;
|
||
|
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
|
||
|
}
|
||
|
|
||
|
.btn-submit {
|
||
|
background-color: #007bff;
|
||
|
color: white;
|
||
|
}
|
||
|
|
||
|
.btn-submit:hover {
|
||
|
background-color: #0056b3;
|
||
|
}
|
||
|
|
||
|
.alert {
|
||
|
margin-bottom: 20px;
|
||
|
}
|
||
|
|
||
|
.form-label {
|
||
|
font-weight: bold;
|
||
|
}
|
||
|
</style>
|
||
|
<script>
|
||
|
// Function to get user location and update map
|
||
|
function getLocation() {
|
||
|
if (navigator.geolocation) {
|
||
|
navigator.geolocation.getCurrentPosition(showPosition, showError);
|
||
|
} else {
|
||
|
alert("Geolocation is not supported by this browser.");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function showPosition(position) {
|
||
|
const latitude = position.coords.latitude;
|
||
|
const longitude = position.coords.longitude;
|
||
|
|
||
|
// Update hidden fields with the latitude and longitude
|
||
|
document.getElementById("latitude").value = latitude;
|
||
|
document.getElementById("longitude").value = longitude;
|
||
|
|
||
|
// Update map with user location
|
||
|
var map = L.map('map').setView([latitude, longitude], 13);
|
||
|
|
||
|
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
||
|
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
||
|
}).addTo(map);
|
||
|
|
||
|
L.marker([latitude, longitude]).addTo(map)
|
||
|
.bindPopup("Your Location")
|
||
|
.openPopup();
|
||
|
}
|
||
|
|
||
|
function showError(error) {
|
||
|
switch (error.code) {
|
||
|
case error.PERMISSION_DENIED:
|
||
|
alert("User denied the request for Geolocation.");
|
||
|
break;
|
||
|
case error.POSITION_UNAVAILABLE:
|
||
|
alert("Location information is unavailable.");
|
||
|
break;
|
||
|
case error.TIMEOUT:
|
||
|
alert("The request to get user location timed out.");
|
||
|
break;
|
||
|
case error.UNKNOWN_ERROR:
|
||
|
alert("An unknown error occurred.");
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Trigger location fetching when the page loads
|
||
|
window.onload = getLocation;
|
||
|
</script>
|
||
|
</head>
|
||
|
|
||
|
<body>
|
||
|
<?php include 'includes/header.php'; ?>
|
||
|
|
||
|
<div class="container">
|
||
|
<h2 class="text-center mb-4">Donate Materials</h2>
|
||
|
|
||
|
<!-- Display success or error messages -->
|
||
|
<?php if (isset($success_message)): ?>
|
||
|
<div class="alert alert-success">
|
||
|
<?php echo $success_message; ?>
|
||
|
</div>
|
||
|
<?php endif; ?>
|
||
|
|
||
|
<?php if (isset($error_message)): ?>
|
||
|
<div class="alert alert-danger">
|
||
|
<?php echo $error_message; ?>
|
||
|
</div>
|
||
|
<?php endif; ?>
|
||
|
|
||
|
<form method="POST" action="material_donations.php">
|
||
|
<div class="mb-3">
|
||
|
<label for="item_name" class="form-label">Item Name</label>
|
||
|
<input type="text" name="item_name" id="item_name" class="form-control" required>
|
||
|
</div>
|
||
|
|
||
|
<div class="mb-3">
|
||
|
<label for="condition" class="form-label">Condition</label>
|
||
|
<select name="condition" id="condition" class="form-select" required>
|
||
|
<option value="">Select Condition</option>
|
||
|
<option value="New">New</option>
|
||
|
<option value="Good">Good</option>
|
||
|
<option value="Used">Used</option>
|
||
|
</select>
|
||
|
</div>
|
||
|
|
||
|
<div class="mb-3">
|
||
|
<label for="quantity" class="form-label">Quantity</label>
|
||
|
<input type="number" name="quantity" id="quantity" class="form-control" min="1" required>
|
||
|
</div>
|
||
|
|
||
|
<!-- Hidden fields to store latitude and longitude -->
|
||
|
<input type="hidden" name="latitude" id="latitude" required>
|
||
|
<input type="hidden" name="longitude" id="longitude" required>
|
||
|
|
||
|
<div id="map" class="map-container"></div>
|
||
|
|
||
|
<div class="d-flex justify-content-center mt-4">
|
||
|
<button type="submit" class="btn btn-submit px-4 py-2">Submit Donation</button>
|
||
|
</div>
|
||
|
</form>
|
||
|
</div>
|
||
|
|
||
|
<?php include 'includes/footer.php'; ?>
|
||
|
|
||
|
<!-- Leaflet JS for map functionality -->
|
||
|
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
|
||
|
</body>
|
||
|
|
||
|
</html>
|