Join our forum :) ~Mike30

PHP Company Profile Manager Tutorial

Go back to: Company Profile Manager

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.

Explanation:

The application consists of three main parts:

  1. index.php: This file contains the HTML form where users can input the company profile information.
  2. save_profile.php: This file handles the form submission, validates the input, and stores the information in a session.
  3. Display Profiles (optional): A separate page that displays the saved profiles.

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.

Code:

Step 1: Create the Form (index.php)

<?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>
    

Step 2: Handle Form Submission (save_profile.php)

<?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!";
}
?>
    

Step 3: Display Saved Profiles (optional)

<?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>