This tutorial will guide you through creating a simple PHP application to manage company profiles. The application will include a form for inputting company information, handle form submissions, and store the data in a session. Additionally, we'll provide an optional step to display the saved profiles.
The application consists of three main parts:
This is a basic example and can be extended further. For example, you could store the data in a database, add more validation, or improve the user interface.
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Company Profile Manager</title>
</head>
<body>
<h1>Company Profile Manager</h1>
<form action="save_profile.php" method="post">
<label for="company_name">Company Name:</label><br>
<input type="text" id="company_name" name="company_name" required><br><br>
<label for="daily_production_rate">Daily Production Rate:</label><br>
<input type="number" id="daily_production_rate" name="daily_production_rate" required><br><br>
<label>Product Features:</label><br>
<input type="checkbox" name="features[]" value="Feature A"> Feature A<br>
<input type="checkbox" name="features[]" value="Feature B"> Feature B<br>
<input type="checkbox" name="features[]" value="Feature C"> Feature C<br><br>
<label>Owner Gender:</label><br>
<input type="radio" name="gender" value="Male" required> Male<br>
<input type="radio" name="gender" value="Female" required> Female<br><br>
<label for="delivery_time">Delivery Time (YYYY/MM/DD):</label><br>
<input type="date" id="delivery_time" name="delivery_time" required><br><br>
<input type="submit" value="Save Profile">
</form>
</body>
</html>
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$company_name = htmlspecialchars($_POST['company_name']);
$daily_production_rate = htmlspecialchars($_POST['daily_production_rate']);
$features = isset($_POST['features']) ? $_POST['features'] : [];
$gender = htmlspecialchars($_POST['gender']);
$delivery_time = htmlspecialchars($_POST['delivery_time']);
// Validate date format
$date_format = 'Y-m-d';
$d = DateTime::createFromFormat($date_format, $delivery_time);
if (!($d && $d->format($date_format) === $delivery_time)) {
echo "Invalid date format. Please enter the date in the format YYYY-MM-DD.";
exit;
}
// Store the information in a session
$_SESSION['company_profiles'][$company_name] = [
"Daily Production Rate" => $daily_production_rate,
"Product Features" => $features,
"Owner Gender" => $gender,
"Delivery Time" => $delivery_time,
];
echo "Profile for $company_name saved successfully!";
}
?>
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Company Profiles</title>
</head>
<body>
<h1>Saved Company Profiles</h1>
<?php
if (isset($_SESSION['company_profiles'])) {
foreach ($_SESSION['company_profiles'] as $company_name => $profile) {
echo "<h2>$company_name</h2>";
echo "<p>Daily Production Rate: " . $profile['Daily Production Rate'] . "</p>";
echo "<p>Product Features: " . implode(', ', $profile['Product Features']) . "</p>";
echo "<p>Owner Gender: " . $profile['Owner Gender'] . "</p>";
echo "<p>Delivery Time: " . $profile['Delivery Time'] . "</p>";
}
} else {
echo "<p>No profiles saved yet.</p>";
}
?>
</body>
</html>