<?php
include_once "Functions/get_content.php"; // needs to be included, to be able to use getPageContent()
$options = array('welcome', 'contact', 'T3sT');
if (!isset($_POST['DATA_pages'])) {
$_POST['DATA_pages'] = $options[0];
}
if (!in_array($_POST['DATA_pages'], $options)) {
$_POST['DATA_pages'] = $options[0];
}
$pageName = $_POST['DATA_pages'];
if (isset($_POST['BUTTON_save'])) {
$updatedContent = $_POST['DATA_websiteContent'];
$sql = "UPDATE `page` SET `content` = ? WHERE `pageName` = ?";
$stmt = mysqli_prepare($dbc, $sql);
mysqli_stmt_bind_param($stmt, "ss", $updatedContent, $pageName);
mysqli_stmt_execute($stmt);
echo "<p>Changes saved successfully!</p>";
}
// Get page content
$pageData = getPageContent($dbc, $pageName);
$pageExists = !empty($pageData);
?>
<form method="POST" id="form-web-page-contents">
<label for="sel-pages">Please select a page to edit:</label>
<select name="DATA_pages" id="sel-pages" onchange="document.getElementById('form-web-page-contents').submit();">
<?php
foreach($options as $option) {
echo '<option value="' . $option . '"';
if ($_POST['DATA_pages'] == $option) echo ' selected="selected"';
echo '>' . $option . '</option>';
}
?>
</select>
<br>
<?php if($pageExists): ?>
<textarea name="DATA_websiteContent" rows="20" cols="70"><?= $pageData['content'] ?? '' ?></textarea>
<br>
<button type="submit" name="BUTTON_save">Save</button>
<?php else: ?>
<p style="color: red;">The selected page does not exist in the database.</p>
<?php endif; ?>
</form>