37032-vm/order_handler.php
2025-12-18 07:14:58 +00:00

39 lines
1.3 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
header('Content-Type: application/json');
$response = ['status' => 'error', 'message' => 'An unknown error occurred.'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data['companyName'], $data['contactPerson'], $data['email'], $data['phone'], $data['quantity'], $data['deliveryAddress'])) {
try {
$pdo = db();
$stmt = $pdo->prepare(
'INSERT INTO orders (company_name, contact_person, email, phone, quantity, delivery_address) VALUES (?, ?, ?, ?, ?, ?)'
);
$stmt->execute([
$data['companyName'],
$data['contactPerson'],
$data['email'],
$data['phone'],
$data['quantity'],
$data['deliveryAddress']
]);
$response['status'] = 'success';
$response['message'] = 'Your order has been placed successfully!';
} catch (PDOException $e) {
$response['message'] = 'Database error: ' . $e->getMessage();
}
} else {
$response['message'] = 'Invalid input. Please fill out all fields.';
}
} else {
$response['message'] = 'Invalid request method.';
}
echo json_encode($response);