<?php
include('includes/header.php');
// Enable all error reporting for debugging
error_reporting(E_ALL);
echo('<h1>Official Supply Tracking Form</h1>');
// Handle variables passed via POST ternary operators
$step = isset($_POST['step']) ? $_POST['step'] : 0;
$emp_name = isset($_POST['emp_name']) ? $_POST['emp_name'] : '';
$emp_id = isset($_POST['emp_id']) ? $_POST['emp_id'] : '';
$location = isset($_POST['location']) ? $_POST['location'] : '';
$supply_req = isset($_POST['supply_req']) ? $_POST['supply_req'] : '';
$qty_stock = isset($_POST['qty_stock']) ? $_POST['qty_stock'] : '';
$req_date = isset($_POST['req_date']) ? $_POST['req_date'] : '';
// Sanitize inputs to prevent cross-site scripting (XSS)
$step = htmlentities($step);
$emp_name = htmlentities($emp_name);
$emp_id = htmlentities($emp_id);
$location = htmlentities($location);
$supply_req = htmlentities($supply_req);
$qty_stock = htmlentities($qty_stock);
$req_date = htmlentities($req_date);
if ($step == 0) // First time loading or resetting the form
{
?>
<form action="my-strings.php" method="POST" class="pad">
<p>
<label for="emp_name">Employee Name</label><br>
<input type="text" size="36" id="emp_name" name="emp_name" value="">
</p>
<p>
<label for="emp_id">Employee ID</label><br>
<input type="text" size="36" id="emp_id" name="emp_id" value="">
</p>
<p>
<label for="location">Location</label><br>
<input type="text" size="36" id="location" name="location" value="">
</p>
<p>
<label for="supply_req">Supply Request</label><br>
<input type="text" size="50" id="supply_req" name="supply_req" value="">
</p>
<p>
<label for="qty_stock">Quantity in Stock</label><br>
<input type="number" size="10" id="qty_stock" name="qty_stock" value="">
</p>
<p>
<label for="req_date">Date</label><br>
<input type="date" id="req_date" name="req_date" value="">
</p>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Submit Supply Request">
</form>
<?php
}
else // Display the captured form data
{
echo('<p>Supply Form</p>');
echo('<pre>');
// Read the entire template file into a single string
if (file_exists('office-form.txt')) {
$fcontents = file_get_contents('office-form.txt');
// Replace template placeholders with sanitized user input
$fcontents = str_replace("[NAME]", $emp_name, $fcontents);
$fcontents = str_replace("[ID]", $emp_id, $fcontents);
$fcontents = str_replace("[LOCATION]", $location, $fcontents); // Fixed placeholder bracket typo
$fcontents = str_replace("[SUPPLY]", $supply_req, $fcontents);
$fcontents = str_replace("[STOCK]", $qty_stock, $fcontents);
$fcontents = str_replace("[DATE]", $req_date, $fcontents);
// Output the modified contents to the page
echo $fcontents;
} else {
echo "Error: office-form.txt template file not found.";
}
echo('</pre>');
// Provide form execution option to reset and try again
?>
<form action="my-strings.php" method="POST">
<input type="hidden" name="step" value="0">
<input type="submit" name="submit" value="Try Again">
</form>
<?php
}
include('includes/footer.php');
?>