Code Lab Pro

php inquiry form source code

php inquiry form source code

Introduction:

An inquiry form is a valuable tool for businesses and organizations, enabling them to collect information from potential customers or clients efficiently. Whether you’re running a small business, a service provider, or an e-commerce platform, having an inquiry form on your website can streamline the process of gathering and responding to customer queries. In this tutorial, we’ll walk you through the creation of a PHP inquiry form using HTML for the structure, CSS for styling, and PHP for processing and handling The inquiry form serves as a bridge between users and businesses, allowing visitors to submit questions, request information, or express interest in products or services. The form typically includes fields for the user’s name, email address, subject, and message. HTML provides the foundational layout of the form, while CSS enhances its appearance, ensuring it aligns with your website’s design and is easy to use. PHP handles the backend logic, processing the form data, validating inputs, and sending the inquiries to the designated recipient.form submissions. php inquiry form source code

HTML Code

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Inquiry Form</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .inquiry-form {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 600px;
        }
        .inquiry-form h2 {
            margin-bottom: 15px;
            font-size: 24px;
            color: #333;
            text-align: center;
        }
        .inquiry-form input[type="text"],
        .inquiry-form input[type="email"],
        .inquiry-form textarea,
        .inquiry-form input[type="submit"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .inquiry-form textarea {
            height: 100px;
            resize: vertical;
        }
        .inquiry-form input[type="submit"] {
            background-color: #5cb85c;
            color: #fff;
            cursor: pointer;
            font-size: 16px;
        }
        .inquiry-form input[type="submit"]:hover {
            background-color: #4cae4c;
        }
        .inquiry-form .message {
            margin-top: 15px;
            font-size: 14px;
            text-align: center;
        }
        .inquiry-form .error {
            color: #d9534f;
        }
        .inquiry-form .success {
            color: #5bc0de;
        }
    </style>
</head>
<body>

<div class="inquiry-form">
    <h2>Contact Us</h2>
    <?php
    $message = '';
    $error = '';

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = htmlspecialchars($_POST['name']);
        $email = htmlspecialchars($_POST['email']);
        $subject = htmlspecialchars($_POST['subject']);
        $messageText = htmlspecialchars($_POST['message']);

        // Simple validation
        if (!empty($name) && !empty($email) && !empty($subject) && !empty($messageText)) {
            // Email recipient (replace with your email)
            $to = 'your-email@example.com';
            $headers = "From: $email\r\n";
            $headers .= "Reply-To: $email\r\n";
            $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
            $emailBody = "Name: $name\nEmail: $email\nSubject: $subject\n\nMessage:\n$messageText";

            if (mail($to, $subject, $emailBody, $headers)) {
                $message = 'Your inquiry has been sent successfully!';
            } else {
                $error = 'There was an error sending your inquiry. Please try again.';
            }
        } else {
            $error = 'All fields are required!';
        }
    }
    ?>
    <form method="post" action="">
        <input type="text" name="name" placeholder="Name" required>
        <input type="email" name="email" placeholder="Email" required>
        <input type="text" name="subject" placeholder="Subject" required>
        <textarea name="message" placeholder="Your Message" required></textarea>
        <input type="submit" value="Send Inquiry">
    </form>
    <?php
    if ($error) {
        echo "<p class='message error'>$error</p>";
    } elseif ($message) {
        echo "<p class='message success'>$message</p>";
    }
    ?>
</div>

</body>
</html>

Conclusion

Creating a PHP inquiry form is an essential step in enhancing communication between users and businesses. This tutorial has guided you through the process of setting up a functional and aesthetically pleasing inquiry form using HTML, CSS, and PHP. By following these steps, you’ve learned how to design a user-friendly form, style it to match your website’s theme, and implement With the form now in place, users can easily submit their inquiries, and you can efficiently manage and respond to these submissions. The HTML provides the structure, CSS ensures a polished and responsive design, and PHP handles data processing and email notifications. This combination not only improves user experience but also streamlines the process of handling customer queries.backend logic to handle form submissions effectively.
Facebook
Twitter
LinkedIn
WhatsApp
Email
X
Print
Scroll to Top