I have dropdowns in my "Edit.php" file. When the edit option in index.php is clicked, it brings to the form. Some of the input for the forms have dropdowns queried from a table called matrix_dropdowns. What I am trying to achieve is: "Capacity" field has dropdowns namely GREEN, AMBER, RED, and BLACK. If an initial value is GREEN or the user chooses GREEN option, I want the background to be green, AMBER ->yellow color. The codes for edit.php is shown below:
<?php
require_once('include/database.php');
if (isset($_POST['btn_submit'])) {
if (isset($_POST['txt_id'])) {
$id = $_POST['txt_id'];
} else {
$id = '';
}
if (isset($_POST['txt_capacity'])) { ////It has dropdowns, namely GREEN, AMBER, RED, BLACK.
$capacity = $_POST['txt_capacity'];
} else {
$capacity = 0;
}
if (isset($_POST['txt_ownership'])) {
$ownership = $_POST['txt_ownership'];
} else {
$ownership = '';
}
try {
$stmt = $conn->prepare("UPDATE MATRIX SET Capacity=:capacity,
Ownership=:ownership
WHERE OBJECTID =:id");
$stmt->execute(array(':capacity' => $capacity, ':ownership'=>$ownership, ':id' => $id));
if ($stmt) {
header('Location:index.php');
exit();
}
} catch (PDOException $e) {
echo $e->getMessage();
}
}
$object_id='';
$capacity = '';
$ownership = '';
if (isset($_GET['id'])) {
$id = $_GET['id'];
$stmt = $conn->prepare("SELECT * FROM MATRIX WHERE OBJECTID=:id");
$stmt->execute(array(':id' => $id));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$object_id = $row['OBJECTID'];
$capacity = $row['Capacity'];
$ownership = $row['Ownership'];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit the Data</title>
</head>
<body>
<h2>Edit the records</h2>
<form action="" method="post">
<table border="3px" cellpadding="5px">
<tr>
<td>Capacity</td>
<td><label>
<select name="txt_capacity" class="textfields" id="capacity">
<option id="0">Select One</option>
<?php
require_once('include/database.php');
$stmt = $conn->prepare("SELECT * FROM MATRIX_DROPDOWNS");
$stmt ->execute();
$result = $stmt->fetchAll();
foreach($result as $row){
?>
<option id="<?=$row['OBJECTID'];?>"><?=$row['colors']?></option>
<?php
}
?>
</select>
</label>
</td>
</tr>
<tr>
<td>Ownership</td>
<td><label>
<select name="txt_ownership" class="textfields" id="ownership">
<option id="0">Select One</option>
<?php
require_once('include/database.php');
$stmt = $conn->prepare("SELECT * FROM MATRIX_DROPDOWNS");
$stmt ->execute();
$result = $stmt->fetchAll();
foreach($result as $row){
?>
<option id="<?=$row['OBJECTID'];?>"><?=$row['colors']?></option>
<?php
}
?>
</select>
</label>
</td>
</tr>
<tr>
<td><label>
<input type="hidden" name="txt_id" value="<?= $object_id; ?>">
</label>
</td>
<td><label><input type="submit" name="btn_submit" value="Submit">
</label>
</td>
</tr>
</table>
</form>