0

I'm trying to send form data to my E-mail with no success. I want that when the user click on "submit" button (and after validation) the data field will send to my e-mail and the user see a message "Thank you for contacting us" instead of the form field. I am very new with PHP and hope one of you can help me.

This is my HTML form:

    <form id="contactForm" method="post" action="contact.php" onsubmit="return validateForm()">
            <input type="text" name="name" id="name" placeholder="Full Name">

            <input type="email" name="email" id="email" placeholder="E-mail Address">

            <input type="text" name="phone" id="phone" placeholder="Phone Number">

            <input type="text" name="company" id="company" placeholder="Company">

            <textarea name="message" id="message" placeholder="Write Your Message Here..."></textarea>

            <input type="submit" value="SEND" id="submitForm">
</form>

and this is my PHP Code:

<?php

if($_POST["submit"]) {
    $recipient="myEmail@gmail.com";
    $subject="Message from Website";
    $sender=$_POST["name"];
    $senderEmail=$_POST["email"];
    $senderPhone=$_POST["phone"];
    $senderCompany=$_POST["company"];
    $message=$_POST["message"];

    $mailBody="Name: $sender\nEmail: $senderEmail\nPhone: $senderPhone\nCompany: $senderCompany\n\n$message";

    mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");

$thankYou="<p>Thank you! Your message has been sent.</p>";
}

?>

I also have JS validation function to this form called validateForm() which works fine. Should I need to define something on the hosting server to make this PHP works?

user3355028
  • 177
  • 2
  • 4
  • 11

3 Answers3

1

Add the following lines to the top of your script

error_reporting(E_ALL);
ini_set("display_errors", "On");

If your server is not configured correctly with mail you will see errors. The best practice is to use PHPMailer. With that you can use sendmail or any SMTP server

lePunk
  • 523
  • 3
  • 10
  • Thanks, now I get this error: Notice: Undefined index: submit in /home/mywebsite/public_html/contact.php on line 7 line 7 is: if($_POST["submit"]) { – user3355028 Feb 13 '15 at 10:46
  • if (isset($_POST['submit']) && $_POST['submit']){ but that is unrelated to your mailing problem – lePunk Feb 13 '15 at 11:34
0

Have you tried using something like PHPMailer?

https://github.com/PHPMailer/PHPMailer/

Its a class that'll send the email with the appropriate headers and is both easy to use and debug.

You'll also find that since it has the appropriate headers, spam filters wont stop it from reaching the correct destination.

Hope it helps.

Hazonko
  • 1,025
  • 1
  • 15
  • 30
0

use this

<input type="submit" value="SEND" id="submit">

UPDATE:

It is the name="xx" attribute of the input button that is used to give a variable name to the values passed on the $_POST or $_GET arrays

So you need to add a name="submit" to the button html tag like so

<input type="submit" value="SEND" name="submit">

Now this PHP statement will work as it will see a named variable called 'submit' in the $_POST array

if ( $_POST['submit'] )
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67
  • 1
    Please try to explain why your answer will resolve the OP problems, reading it as is let newcomers/beginners wondering why id should be submit and not something else ? – Tensibai Feb 13 '15 at 12:40