0

I'm trying to link my HTMl form to my PHP contact code but it doesn't work, no mail is sending. I know all the PHP settings on the server are ok, I've done a test with a single-page-form and I received a mail.

Do you know what could be wrong ?

Thank you very much!

HTML CODE :

<section id="contact-full">
  <div class="container">
   <h2 class="title">Message</h2>
  
   <form action="scripts/contact.php" role="form" id="contact_form" novalidate>
    <div class="form-group">
     <input type="text" class="form-control" id="contact_name" placeholder="Full name" name="name">
    </div>
    <div class="form-group">
     <input type="email" class="form-control" id="contact_email" placeholder="Email Address" name="email">
    </div>
    <div class="form-group">
     <textarea class="form-control" rows="4" placeholder="Your message or question" id="contact_message" name="message"></textarea>
    </div>
    <button type="submit" id="contact_submit" data-loading-text="•••" class="btn btn-lg btn-block btn-primary"><i class="icon icon-paper-plane"></i>Send</button>
   </form>
  </div>
 </section>

PHP CODE :

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(!empty($_POST['contactname']) && !empty($_POST['contactemail']) && !empty($_POST['contactmessage'])) {
    $to = 'your@email.com'; // Your e-mail address here.
    $body = "\nName: {$_POST['contactname']}\nEmail: {$_POST['contactemail']}\n\n\n{$_POST['contactmessage']}\n\n";
    mail($to, "Message from yoursite.com", $body, "From: {$_POST['contactemail']}"); // E-Mail subject here.
    }
}
?>
Jonnix
  • 4,121
  • 1
  • 30
  • 31

1 Answers1

0

You're missing a few things;

First off in your HTML code you need to add method = "POST" to your <form> tag.

Secondly in your PHP code you grab all the incorrect names with $_POST. Change the following:

if(!empty($_POST['contactname']) && !empty($_POST['contactemail']) && !empty($_POST['contactmessage']))

to

if(!empty($_POST['contact_name']) && !empty($_POST['contact_email']) && !empty($_POST['contact_message']))

And all other parts of the code that call those objects.

Aroic
  • 479
  • 2
  • 12
  • Thank you very much ! I just don't get why the $to object is causing problem ? If I use the contact_email , the mail will be sent to ... the sender, no ? – Sylvain P. Jun 20 '16 at 17:13
  • Ah that's correct. I wasn't sure how you were using the `contact_email` variable but I see now it's meant to be the return email. My mistake – Aroic Jun 20 '16 at 17:16
  • No prob. But still seems not to work :-/ – Sylvain P. Jun 20 '16 at 17:25
  • Who are you trying to email this message to? Currently it just send to `'your@email.com'` – Aroic Jun 20 '16 at 17:26
  • Yes, I changed the $to mail in the real .php file. Just not wanted to share my mail here – Sylvain P. Jun 20 '16 at 17:27
  • Oh that makes more sense. Ok then, and you changed every single `$_POST` variable as I indicated above? Assuming those are correct, you should change from `!empty` to [isset()](http://php.net/manual/en/function.isset.php) – Aroic Jun 20 '16 at 17:30
  • Yes I changed all the $_POST. So it will be like : if(isset($_POST['contact_name']) && isset($_POST['contact_email']) && isset($_POST['contact_message'])) ? – Sylvain P. Jun 20 '16 at 17:36
  • Yes, I've had much more success with that than with empty. Let me know how it goes. – Aroic Jun 20 '16 at 17:44
  • Sorry but still no anwser ! I got a custom.js file where there are lines like contactemail: $('#contact_email').val(), Is it supposed to convert contactemail to contact_email so I should left it as default, contactemail ? – Sylvain P. Jun 20 '16 at 18:01
  • That js file may be breaking it for you then. The method I gave you should work without it. Do you need it for anything else? Try commenting out the the conversion and see if it works. – Aroic Jun 20 '16 at 18:06
  • I need that file as it manages the informations to be transmitted, the pop up to show, etc ... I tried without it but didn't work. But still don't work with ... – Sylvain P. Jun 20 '16 at 18:25