My PHP contact form sends a blank email when someone submits it
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.
Cite
CoursesWeb 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.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 2The 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?
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.
1 answer
- 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.