Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

P M
P M
4,226 Points

PHP Notice: Undefined index

I am getting a PHP notice for undefined index on the line that corresponds to the conditional where $_POST["address"] != ""

if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = trim(filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING)); $email = trim(filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL)); // checks if the email has a value $details = trim(filter_input(INPUT_POST, "details", FILTER_SANITIZE_SPECIAL_CHARS));

if ($name == "" || $email == "" || $details == "") {

    echo "Please fill in the required fields: Name, Email and Details";
    exit;
}

if ($_POST["address"] != "") {
  echo "Bad form input";
  exit;
}

1 Answer

Hi,

maybe try this :) i used empty() function instead of your solutions.

if ($SERVER["REQUEST_METHOD"] == "POST") { 
    $name = trim(filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING)); 
    $email = trim(filter_input(INPUT_POST, "email", FILTERSANITIZE_EMAIL)); 
    // checks if the email has a value 
    $details = trim(filter_input(INPUT_POST, "details", FILTER_SANITIZE_SPECIAL_CHARS));

if (empty($name) || empty($email) || empty($details)) {
    echo "Please fill in the required fields: Name, Email and Details";
    exit;
}
$address = $_POST["address"];
if (!empty($address)) {
  echo "Bad form input";
  exit;
}