-2

I've seen dozens of posts about this issue and it basically comes down to a variable not being declared or given a value. However I'm 100% sure it's the same and declared.

I have a basic contact form in HTML and I want it to send me and e-mail when someone hits the submit button. I am debugging the code as well to see what the problem is. The only issue it can find is that there is an Undefined Index which belongs to my text area.

I know that the name of the textarea must be the same as the name on my $_POST in the PHP. Please take a look at the two sections of code and tell me if you can see why it wouldn't be fetching the information from my textarea. The name is message-area.

HTML

<form action="mail.php" method="post" name=contact-me-form >
     <label name="firstname secondname">Name: *&nbsp;</label><br>
     <input class="half-box" type="text" name="firstname" required >
     <input class="half-box" type="text" name="secondname" required ><br>
     <p class="first-name">First Name</p>
     <p class="second-name">Last Name</p><br>
     <label name="email">Email Address: *&nbsp;</label><br>
     <input class="full-box" type="email" name="email" spellcheck="false" required><br>
     <label name="subject">Subject:&nbsp;</label><br>
     <input class="full-box" type="text" name="subject"><br>
     <label name="message">Message: *&nbsp;</label><br>
     <textarea name="message-area" form="contact-me-form" type="text"  placeholder="Please enter your message"></textarea>
     <button name="submit" type="submit" value="Submit">Submit</button>
</form>

PHP

<?PHP

  $to = "";
  $from = "";
  $first_name = '';
  $last_name = '';
   $subject = '';
  $message = null;

 error_reporting(-1);
 ini_set('display_errors', 'On');
 set_error_handler("var_dump");

if(isset($_POST['submit'])){
      $to = 'email@test.com';
      $from = $_POST['email'];
      $first_name = $_POST['firstname'];
      $last_name = $_POST['secondname'];
      $subject = $_POST['subject'];
      $message = $_POST["message-area"];
if($message == null){echo "no message detected";}

 $headers = "From: " . $from;
 $headers = "From:" . $to;
 mail($to,$subject,$message,$headers);
 }

 ?>

As you can see the names are identical yet when I submit the data it comes up displaying the following.

int(8) string(29) "Undefined index: message-area" string(58) "/hermes/bosnaweb25a/b2294/ followed by a bit more information and my error gets displayed: ["message"]=> NULL } no message detected.

I honestly have no idea why this isn't being picked up, can anyone with more experience highlight my mistake?

EDIT 1

This is not to do with dashes/hyphens as I've edited my code as you can see below. It's also important to note that if I change this to raw text it still doesn't work, still acts as if there is no data from the textarea.

HTML

                    <form action="mail.php" method="post" id=contact-me-form >
                    <label name="firstname secondname">Name: *&nbsp;</label><br>
                    <input class="half-box" type="text" name="firstname" required >
                    <input class="half-box" type="text" name="secondname" required ><br>
                    <p class="first-name">First Name</p>
                    <p class="second-name">Last Name</p><br>
                    <label name="email">Email Address: *&nbsp;</label><br>
                    <input class="full-box" type="email" name="email" spellcheck="false" required><br>
                    <label name="subject">Subject:&nbsp;</label><br>
                    <input class="full-box" type="text" name="subject"><br>
                    <label name="message">Message: *&nbsp;</label><br>
                    <textarea name="messagearea" type="text"  placeholder="Please enter your message"></textarea>
                    <button name="submit" type="submit" value="Submit">Submit</button>
                </form>

PHP

<?PHP

$to = "";
$from = "";
$first_name = '';
$last_name = '';
$subject = '';
$message = null;

error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");

 if(isset($_POST['submit'])){
   $to = 'harry.brockley@hotmail.co.uk';
   $from = $_POST['email'];
   $first_name = $_POST['firstname'];
   $last_name = $_POST['secondname'];
   $subject = $_POST['subject'];
   $message = $_POST["messagearea"];
if($message == null){echo "no message detected";}

$headers = "From: " . $from;
$headers = "From:" . $to;
mail($to,$subject,$message,$headers);
}

 ?>

EDIT 2

Tested it with a hard coded value works so it has to be the variable name. It's just strange that it only happens on the textarea.

CardDeath
  • 132
  • 11
  • Tip: if some items *are* being submitted and others aren't, take a close look at what the difference is between them, and remove that difference. – deceze Jul 20 '16 at 08:41
  • Try renaming the `message-area` to a name without a hyphen. – Marki555 Jul 20 '16 at 08:58
  • Thanks guys, took a look. I've removed the hyphen/dash from my HTML and PHP but still the same error just now says "Undefined index: messagearea" Shame still getting the problem. – CardDeath Jul 20 '16 at 09:24

3 Answers3

0

You seem to have a random form="" attribute in the <textarea></textarea>

If you change it to the following it should work.

<textarea name="message-area" type="text"  placeholder="Please enter your message"></textarea>

With respect to the check on $message not working with ($message == null), you should use empty() to check it as that will catch NULL and "".

if (empty($message)) {
    echo "no message detected";
}
Carl Casbolt
  • 102
  • 6
  • In general a `form` attribute is not an issue; it's only an issue when the form the attribute is referencing doesn't exist. In this case, it's because the form has `name=contact-me-form`, when it needs to be an *`id`*. – So, lacking explanation, but it *is* the issue. – deceze Jul 20 '16 at 08:39
  • 1
    Removed the form attribute from the text area and I changed the contact-me-form to an id but still no result. – CardDeath Jul 20 '16 at 09:25
0

Here is your solution just replace below line with your text-area code.

 <textarea name="message-area"   placeholder="Please enter your message"></textarea>

here it is no need form="contact-me-form" text.

Denis Bhojvani
  • 817
  • 1
  • 9
  • 18
0

Thank you for your help and I found out the problem and it seems to work. I haven't actually received the e-mail yet (Possibly terrible Vietnamese Internet).

The problem was that my variables defined as $variable = "" were not set as null as I thought. I had to go through and assign = null to all of my variables and it seemed to bypass the error.

CardDeath
  • 132
  • 11