简体   繁体   中英

Broken PHP contact form script

I'm trying to make a contactform. And somehow it won't work. I tried to send a email just with the mail()-function and that works. So I'm doing something wrong and I don't know what.

When you click on the submit button, I refer to the mailer.php file. Both file are in the same folder.

<?php
if(isset($_POST['sendButton']))
{
 $to = "contact@domain.com";
 $subject = "Contactformulier Portfolio";
 $name_field = $_POST['yourName'];
 $email_field = $_POST['yourEmail'];
 $message = $_POST['yourMessage'];

 $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

 echo "Data has been submitted to $to!";
 mail($to, $subject, $body);
}
else
{
 echo "There has been some error, try again please!";
}
?>

Is your form button named "sendButton"? <input type='submit' name='sendButton' /> If not, there's your culprit.

Can you provide your HTML as well?

EDIT:

$_POST indeces are defined by the name attribute on your input elements. As you have only defined an id field on your submit input element, the condition you are checking is never met (and thus the mail is never send). Add the name attribute to your submit input element like this:

Without seeing all your code, my guess is that the condition for mailing is never met because there is no value set for the submit button. Try making the condition for emailing like this:

if($_POST) {
  Mail Here
} else {
  error case
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM