Im trying to create a contact form using php,and having some troubles.
the condition: isset($_POST['submit'])
always produces false, even if I just submit a blank page.
here is my code:
contact.html part:
<form action='emailto.php' method='POST' enctype='text/plain'>
First Name:<br>
<input type='text' name='firstname'><br>
Last Name:<br>
<input type='text' name='lastname'><br>
Email Address:<br>
<input type='text' name='emailadd' ><br>
Subject:<br>
<input type='text' name='subject' ><br>
Message:<br>
<textarea name='message' rows=5 cols=30 ></textarea><br><br>
<input type='submit' value='Send' name='submit'>
</form>
emailto.php:
<?php
if (isset($_POST['submit'])) {
$to = "emailaddress";
$subject = $_POST['subject'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$Emailadd = $_POST['emailadd'];
$Message = $_POST['message'];
$Body= "";
$Body .= $firstname;
$Body .= $lastname;
$Body .= "\n";
$Body .= $Emailadd;
$Body .= "\n";
$Body .= $Message;
mail($to,$subject,$Body);
echo "Mail Sent! <a href='contact.html'>Go Back</a>";
} else {
header("Location: contact.html");
exit(0);
}
?>
Besides, what's weird is that if I delete the if-else statement in emailto.php, after submitting, an error message will occur: undifined index: subject, firstname, lastname, emailadd, message...
I was totally confused.. Looking forward to hear some advice. Thanks in advance.
isset()
returns true for a variable defined as empty string (such as a blank POST data field). – Markus AO