3

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.

Martin
  • 22,212
  • 11
  • 70
  • 132
Nicole Yan
  • 53
  • 7
  • Also be aware that `isset()` returns true for a variable defined as empty string (such as a blank POST data field). – Markus AO Dec 31 '16 at 21:41

1 Answers1

2

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      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\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);
}
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 2
    Something like: http://stackoverflow.com/questions/7628249/method-post-enctype-text-plain-are-not-compatible – Progrock Dec 31 '16 at 21:52
  • @Progrock Yes. However, I added a few things in my answer in order to help them out. Hopefully everything will go well and as intended. – Funk Forty Niner Dec 31 '16 at 21:57
  • THX! Really Helpful! – Nicole Yan Dec 31 '16 at 22:21
  • @NicoleYan You may like to mark the question/answer as solved in order to inform everyone/the Stack system that a solution was found. Otherwise it will be considered as "still open/unsolved" and others may think that they should post more answers. *Welcome to Stack* – Funk Forty Niner Dec 31 '16 at 22:29