I need to include a simple contact form in either the sidebar or footer of an existing website so it appears on every page.

This is not a WordPress site, I have seen there are several plugins for WordPress. I need a different solution. I am thinking a php include would be my best option. However, my php skills are minimal. Can someone out there point me in the right direction? Any advice would be much appreciated.

I have searched all day and have yet to find a good solution.

Dani AI

Generated

A server-side fragment is the simplest, most robust way to put a contact form into a site-wide sidebar/footer — ' answer points in the right direction. Which embedding method to use depends on the site: if pages are served as PHP a small partial that posts to a single handler is ideal; if files are static HTML, either configure the server to parse .html as PHP, load the fragment with client-side fetch/AJAX, or use an isolated iframe as a last resort.

Key practical points not shown above:

  • Use a single handler URL (root‑relative like /contact/submit.php) so the form action works from any directory.
  • Keep the fragment markup minimal and accessible (labels, required attributes) and avoid inline PHP output if a plain HTML file can be included.
  • Protect the handler: validate/sanitize inputs, block header-injection, verify a CSRF token stored in session, and return a simple success/redirect page.

Example of a minimal server-side handler (showing validation, basic anti-header-injection and redirect):

<?php
session_start();
if (!hash_equals($_SESSION['csrf_token'] ?? '', $_POST['token'] ?? '')) { http_response_code(400); exit; }
$name  = preg_replace('/[\r\n]+/',' ', trim($_POST['name'] ?? ''));
$email = filter_var($_POST['email'] ?? '', FILTER_VALIDATE_EMAIL);
$msg   = trim($_POST['message'] ?? '');
if (!$email || $msg === '') { http_response_code(400); exit; }
$headers = "From: noreply@example.com\r\nReply-To: $email\r\n";
if (mail('owner@example.com', 'Contact form', "From: $name <$email>\n\n$msg", $headers)) {
  header('Location: /thank-you.html'); exit;
}
http_response_code(500);

Operational tips: use PHPMailer (SMTP) for reliable delivery, add a CAPTCHA or rate-limiter to reduce spam, log submissions or store them in a DB with prepared statements, and test on a staging server while checking mail and PHP error logs. These items close common gaps that cause form failures when the form is included site-wide.

Recommended Answers

All 2 Replies

You'll just need to include a file that will output your form, something like:

// small_form.php
echo <<<EOT
    <form method="post" action="your_form_handler.php">
        <!-- your inputs here -->
        <input type="submit" />
    </form>
EOT;

Then you can just:

include 'small_form.php';

where needed.

Thanks for the info. I will give it a try. I will probably be posting more or bothering you again next week when I begin this project.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.