3
votes

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.

1
Also be aware that isset() returns true for a variable defined as empty string (such as a blank POST data field).Markus AO

1 Answers

2
votes

Remove this enctype='text/plain' from your form and it will start working; I guarantee it. And if it fails, then you need to find out why that is and making sure that mail is available for you to use.

Sidenote: You should also check if any of the inputs are empty (and required which helps).

and use proper (full) headers for mail():

Otherwise, your mailout may be treated as spam or rejected altogether.

There should be a valid From: <email> as part of mail's 4th argument.

I.e. and from the manual:

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

"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..."

  • Again; this is caused by enctype='text/plain' in the form which isn't a valid enctype for using the POST array.

Edit:

Add an if/else to mail(). If it echos "Houston we have a problem", then there's a problem on your end.

If it echo "Mail Sent!" but no mail is received, then look at your spam box. Mail has done its job and you need to find out why it was never sent/received.

<?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;

if(mail($to,$subject,$Body)){
echo "Mail Sent! <a href='contact.html'>Go Back</a>";
} else { echo "Houston, we have a problem"; }

} else {
header("Location: contact.html");
exit(0);
}
?>