Notice: Undefined index [closed]

Try using the first code – that worked, but gave errors when submitted the form blank and just cahnge isset($_POST['name'] to:

isset($_POST['name'] && !empty($_POST['name']

In your code you are triggering the $missing_content it the vars are empty. According to the spec (see reference) an empty (so not filled as in your case) string will trigger this. Also a string is considered empty if it is not set so in adition to the aforementioned code I would suggest to first set all the values first.

Reference:

Now personaly I would change your code to look something like this (please bare in mind that it’s totally late here and I’m after 10 hours of coding so I’ll just give you a piece to start):

First set some important for you vars that has to be filled by user and will help you to verify the form. I’ll use name, email and text.

$error_name = false;
$error_email = false;
$error_message = false;

I’m setting them to false since on start we assume everything is ok and the user will fill everything. Then we set as well as get the values. Now you didn’t post your DOM for the form but I assume your submit button has the submitted name and ID.

if (isset($_POST['submitted'])) {
    $name="";
    $email="";
    $message="";

So after the form is submitted the values are set. Sure they are all empty right now but now at least we will not have issues with values missing and not set. Because of that we can omit later on isset because we are sure they are indeed set. Now let’s get some real values so here’s the continuation of above code:

if (trim($_POST['name']) === '') {
    $error_name = true;
} else {
    $name = trim($_POST['name']);
}

What happens here is we check if the value is set to nothing. And since we did that ourselves at the beginning then if the user didn’t input anything we can be sure that there was a field missing. We also use trim so if someone uses only spaces or tabs etc. it will trim those so the value would still be nothing. If so then we set the $error_name to be true. We will use that later. Now if the value is not equal to nothing then we know the user really took the effort to input their name so we assign the value. Of course this is just one part. You would have to do this three times for each value (name, email and message).

Ok we are all set. We have the values the user sent and even if they didn’t input anything we at least know we have set them to nothing and we have error vars set to true if such thing happened. Time to use those error vars:

if (!$error_name && !$error_email && !$error_message) {
    //rest of your form sending procedure goes here
} else {
    $contact_form_not_filled = true;
}

So in the end we check if all error vars are false. If so we can proceed and send the form if not we set other var to true which we can then use to trigger for example an AJAX message or just a simple notification like so:

<?php if (isset($contact_form_not_filled) && $contact_form_not_filled == true) : ?>
    <strong><?php 'Error! ' ?> </strong><?php 'Fill out the form correctly and try again.' ?>
<?php endif; ?>