Skip to content
CoursesWeb Forum

Undefined index warning when the form loads for the first time in PHP

Asked by Lin W. · Error Messages Explained3 repliesupdated

Short answer

Following on from my contact form (thank you to everyone who helped). Now I have an order form on the same page that shows what the customer typed again if something is missing. It works after you press the button. But the very first time the page opens, before anybody has typed anything, I get this yellow line at the top:

Written by Lin W. ·
CiteCoursesWeb Forum, "Undefined index warning when the form loads for the first time in PHP", https://coursesweb.net/forum/t/undefined-index-warning-when-the-form-loads-for-the-first-time-in-php/, accessed 2026-12-02.
  1. Member ·

    Notice: Undefined index: name in /home/site/order.php on line 12

    and one more for "phone". The code on line 12 is:

    <input type="text" name="name" value="<?php echo $_POST['name']; ?>">
    <input type="text" name="phone" value="<?php echo $_POST['phone']; ?>">
    

    After the customer presses Send, the page fills in the fields and the notice is gone. It is only the first visit that shows it.

    I know a notice is not a fatal error, and the page still works, but customers can see it and it looks sloppy for the restaurant. My host runs an older PHP version. What is the correct way to fix this, and not just hide it?

    0
    ReplyLink

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

  1. Zaid M.Member ·

    just put an @ in front, like echo @$_POST['name']; the notice goes away instantly. i do that everywhere in my game code. nobody sees the warnings and the page looks clean, what is the downside?

  2. Ewa P.Member ·

    On the first visit nobody has submitted the form yet, so $_POST is empty and the key "name" does not exist. PHP is warning you that you are reading something that is not there. Check it with isset() before you print it.

  3. Marisa T.Member ·

    A small edge case to keep in mind: if you use empty() instead of isset(), a value of "0" counts as empty. For a name field it will not matter, but for a quantity field it could. Which one do you want here, Lin?

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