Skip to content
CoursesWeb Forum

My PHP contact form sends a blank email when someone submits it

Asked by Lin W. · PHP Forms, Sessions and Basics1 replyupdated

Short answer

I keep a small ordering page alive for my family's restaurant and I added a contact form to it last night. When I fill it in and press Send, the email arrives, but the body is empty. It only says "Name: " and "Message: " with nothing after.

Written by Lin W. ·
CiteCoursesWeb Forum, "My PHP contact form sends a blank email when someone submits it", https://coursesweb.net/forum/t/my-php-contact-form-sends-a-blank-email-when-someone-submits-it/, accessed 2026-11-27.
  1. Member ·

    This is the form:

    <form method="post" action="contact.php">
      <input type="text" id="name">
      <textarea id="message"></textarea>
      <button type="submit">Send</button>
    </form>
    

    And contact.php:

    <?php
    $name = $_POST['name'];
    $message = $_POST['message'];
    mail('orders@example.com', 'Contact form',
         "Name: $name\nMessage: $message", 'From: web@example.com');
    

    The page does not show any error on the screen. Our host writes a log file though, and it says:

    PHP Warning: Undefined array key "name" in /home/site/contact.php on line 2

    The same warning appears for "message". I am scared of changing too much because the ordering page shares the same folder. What does the warning mean and why is the data not arriving?

    0
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

  1. Ewa P.Member ·

    Look at your HTML, the inputs have an id but no name attribute. The browser only sends fields that have a name, so PHP never receives them. Add name="name" and name="message" and try again.

    I would also stop using the raw values in the email headers later on. For now the body is fine.

CoursesWeb Forum. Questions and answers on learning web development: HTML, CSS, JavaScript, PHP, MySQL, Node.js and AI coding help.