101 lines
3.9 KiB
PHP
101 lines
3.9 KiB
PHP
|
|
<?php
|
|
// Correct array syntax and fix PHP errors in edit_provider.php
|
|
// edit_provider.php
|
|
// This page allows editing a provider's details.
|
|
// It expects a provider id passed via the query string: ?id=123
|
|
|
|
$provider_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
|
|
if ($provider_id <= 0) {
|
|
echo '<h1>Error</h1>' .
|
|
'<p>Provider ID is missing or invalid.</p>';
|
|
exit;
|
|
}
|
|
|
|
// API base URL
|
|
$api_base = 'http://127.0.0.1:5000';
|
|
|
|
// Handle form submission
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
// Collect submitted data
|
|
$payload = [
|
|
'name' => $_POST['name'] ?? '',
|
|
'age' => (int)($_POST['age'] ?? 0),
|
|
'photo' => $_POST['photo'] ?? '',
|
|
'phone' => $_POST['phone'] ?? '',
|
|
'rates' => json_decode($_POST['rates'] ?? 'null', true) ?? [],
|
|
'private' => isset($_POST['private']) && $_POST['private'] === 'on',
|
|
];
|
|
|
|
// Send PUT request to API
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $api_base . '/providers/' . $provider_id);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($httpCode >= 400) {
|
|
$error = json_decode($response, true)['error'] ?? 'Update failed';
|
|
echo '<h1>Error</h1><p>' . htmlspecialchars($error) . '</p>';
|
|
} else {
|
|
// Success: redirect back to provider list
|
|
header('Location: list_providers.php');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Fetch current provider details for the form (GET request)
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $api_base . '/providers?id=' . $provider_id);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($httpCode >= 400) {
|
|
echo '<h1>Error</h1><p>Could not load provider details.</p>';
|
|
exit;
|
|
}
|
|
$provider = json_decode($response, true);
|
|
// API may return array for GET by id
|
|
if (is_array($provider) && count($provider) > 0 && isset($provider[0])) {
|
|
$provider = $provider[0];
|
|
}
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Edit Provider</title>
|
|
<link rel="stylesheet" href="https://cosmostat.matt-cloud.com/src/styles.css">
|
|
<style>
|
|
body {font-family: Arial, sans-serif; margin: 20px;}
|
|
form {max-width: 600px;}
|
|
label {display: block; margin-top: 10px;}
|
|
input[type=text], input[type=number], textarea {width: 100%; padding: 8px; box-sizing: border-box;}
|
|
input[type=checkbox] {margin-top: 5px;}
|
|
button {margin-top: 15px; padding: 10px 15px;}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="card">
|
|
<h1>Edit Provider</h1>
|
|
<form method="post" id="editForm">
|
|
<label>Name: <input type="text" name="name" id="name" required value="<?php echo htmlspecialchars($provider['name'] ?? ''); ?>"></label>
|
|
<label>Age: <input type="number" name="age" id="age" required value="<?php echo htmlspecialchars($provider['age'] ?? ''); ?>"></label>
|
|
<label>Photo URL: <input type="text" name="photo" id="photo" value="<?php echo htmlspecialchars($provider['photo'] ?? ''); ?>"></label>
|
|
<label>Phone: <input type="text" name="phone" id="phone" required value="<?php echo htmlspecialchars($provider['phone'] ?? ''); ?>"></label>
|
|
<label>Rates (JSON):<textarea name="rates" id="rates" rows="4"><?php echo htmlspecialchars(json_encode($provider['rates'] ?? [], JSON_PRETTY_PRINT)); ?></textarea></label>
|
|
<label>Private: <input type="checkbox" name="private" id="private" <?php echo (!empty($provider['private'])) ? 'checked' : ''; ?>></label>
|
|
<button type="submit">Save</button>
|
|
<button type="button" onclick="window.location.href='list_providers.php'">Cancel</button>
|
|
</form>
|
|
</div>
|
|
</body>
|
|
</html>
|