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: * </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: * </label><br>
<input class="full-box" type="email" name="email" spellcheck="false" required><br>
<label name="subject">Subject: </label><br>
<input class="full-box" type="text" name="subject"><br>
<label name="message">Message: * </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: * </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: * </label><br>
<input class="full-box" type="email" name="email" spellcheck="false" required><br>
<label name="subject">Subject: </label><br>
<input class="full-box" type="text" name="subject"><br>
<label name="message">Message: * </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.