refactors create/edit form for POIs

This commit is contained in:
Sundog Jones 2024-05-15 11:00:55 -04:00
parent 8171131683
commit 7f30383ef3
1 changed files with 55 additions and 24 deletions

View File

@ -50,27 +50,25 @@
switch ($_REQUEST['action']) { switch ($_REQUEST['action']) {
case 'create': case 'create':
// create POI form // create POI form
?> echo "<h3>Create a New Point of Interest</h3>";
<h3>Create a New Point of Interest</h3> displayForm(false);
break;
<form method='POST' action='pois.php'> case 'edit':
<input type='hidden' name='action' value='save' /> // edit POI form
<p><label for='name'>Name: </label><input type='text' name='name' /></p> echo "<h3>Edit Point of Interest</h3>";
<p><label for='description'>Description: </label><input type='text' name='description' /></p> displayForm(true);
<p><label for='latitude'>Latitude: </label><input type='text' name='latitude' /></p>
<p><label for='longitude'>Longitude: </label><input type='text' name='longitude' /></p>
<p><label for='radius'>Radius: </label><input type='text' name='radius' /></p>
<p><label for='is_active'>Is Active? </label><input type='checkbox' name='is_active' checked /></p>
<p><label for='map_marker_icon'>Map Marker: </label><input type='text' name='map_marker_icon' /></p>
<p><input type='submit' value='Save POI' /></p>
</form>
<?php
break; break;
case 'save': case 'save':
if (isset($_POST['name']) && isset($_POST['latitude']) && isset($_POST['longitude']) && isset($_POST['is_active'])) { if (isset($_POST['name']) && isset($_POST['latitude']) && isset($_POST['longitude']) && isset($_POST['is_active'])) {
// do the insert // do the insert or update
if (!isset($_POST['id'])) {
// insert
$stmt = $conn->prepare('INSERT INTO POIs (name, description, latitude, longitude, radius, is_active, map_marker_icon) VALUES (:name, :description, :latitude, :longitude, :radius, :is_active, :map_marker_icon)'); $stmt = $conn->prepare('INSERT INTO POIs (name, description, latitude, longitude, radius, is_active, map_marker_icon) VALUES (:name, :description, :latitude, :longitude, :radius, :is_active, :map_marker_icon)');
} else {
// update
$stmt = $conn->prepare('UPDATE POIs SET name=:name, description=:description, latitude=:latitude, longitude=:longitude, radius=:radius, is_active=:is_active, map_marker_icon=:map_marker_icon WHERE id = :id');
if ($stmt) $stmt->bindValue(':id', $_POST['id']);
}
if ($stmt) { if ($stmt) {
$stmt->bindValue(':name', $_POST['name'], SQLITE3_TEXT); $stmt->bindValue(':name', $_POST['name'], SQLITE3_TEXT);
$stmt->bindValue(':description', $_POST['description'], SQLITE3_TEXT); $stmt->bindValue(':description', $_POST['description'], SQLITE3_TEXT);
@ -81,7 +79,11 @@
$stmt->bindValue(':map_marker_icon', $_POST['map_marker_icon'], SQLITE3_TEXT); $stmt->bindValue(':map_marker_icon', $_POST['map_marker_icon'], SQLITE3_TEXT);
$result = $stmt->execute(); $result = $stmt->execute();
if ($result) { if ($result) {
if (isset($_POST['id'])) {
echo "<p>Record updated. <a href='pois.php'>&lt; back to Points of Interest</a></p>\n";
} else {
echo "<p>Record created. <a href='pois.php'>&lt; back to Points of Interest</a></p>\n"; echo "<p>Record created. <a href='pois.php'>&lt; back to Points of Interest</a></p>\n";
}
} else { } else {
echo "<p><strong>OOPS!</strong> There was a problem adding the new point of interest!</p>\n"; echo "<p><strong>OOPS!</strong> There was a problem adding the new point of interest!</p>\n";
} }
@ -92,9 +94,6 @@
echo "<strong>Name, Latitude, Longitude are required - please go back and try again</strong>\n"; echo "<strong>Name, Latitude, Longitude are required - please go back and try again</strong>\n";
} }
break; break;
case 'edit':
case 'update':
case 'delete':
default: default:
echo "Unknown action: " . $_GET['action']; echo "Unknown action: " . $_GET['action'];
} }
@ -106,4 +105,36 @@
<?php <?php
require_once(realpath(dirname(__FILE__) . '/../footer.php')); require_once(realpath(dirname(__FILE__) . '/../footer.php'));
// page functions
function displayForm($editMode = false) {
global $conn;
?>
<form method='POST' action='pois.php'>
<input type='hidden' name='action' value='save' />
<?php
if ($editMode) {
$lookupStmt = $conn->prepare('SELECT * FROM POIs WHERE id = :id');
if ($lookupStmt) {
$lookupStmt->bindValue(':id', $_REQUEST['id']);
$lookupResult = $lookupStmt->execute();
if ($lookupResult) {
$poi = $lookupResult->fetchArray();
}
}
echo "<p>ID: " . $_REQUEST['id'] . "</p>\n";
echo "<input type='hidden' name='id' value='" . $_REQUEST['id'] . "' />\n";
}
?>
<p><label for='name'>Name: </label><input type='text' name='name' <?= $editMode ? "value='" . $poi['name'] . "'" : "" ?> /></p>
<p><label for='description'>Description: </label><input type='text' name='description' <?= $editMode ? "value='" . $poi['description'] . "'" : "" ?> /></p>
<p><label for='latitude'>Latitude: </label><input type='text' name='latitude' <?= $editMode ? "value='" . $poi['latitude'] . "'" : "" ?> /></p>
<p><label for='longitude'>Longitude: </label><input type='text' name='longitude' <?= $editMode ? "value='" . $poi['longitude'] . "'" : "" ?> /></p>
<p><label for='radius'>Radius: </label><input type='text' name='radius' <?= $editMode ? "value='" . $poi['radius'] . "'" : "" ?> /></p>
<p><label for='is_active'>Is Active: </label><input type='checkbox' name='is_active' <?= $editMode ? ($poi['is_active'] ? "checked" : "") : "checked" ?> /></p>
<p><label for='map_marker_icon'>Map Marker Icon: </label><input type='text' name='map_marker_icon' <?= $editMode ? "value='" . $poi['map_marker_icon'] . "'" : "" ?> /></p>
<p><input type='submit' value='Save POI' /></p>
</form>
<?php
}
?> ?>