Search code examples
phpformscontacts

Contact form(PHP) Not working


The contact form i am using for my site is not working as intended whenever all the information is correctly filled it still produces the error message. Here are both the files i am using.

Greatly appreciate the help.

config.php

        <?
    //define the receiver of the email
    define('TO_EMAIL','[email protected]');

    //define the subject of the email
    define('SUBJECT','Website Inquiry');    

    // Messages
    define('MSG_INVALID_NAME','Please enter your name.');
    define('MSG_INVALID_EMAIL','Please enter valid e-mail.');
    define('MSG_INVALID_MESSAGE','Please enter your message.');
    define('MSG_SEND_ERROR','Sorry, we can\'t send this message.');

?>

contact.php

    <?php 

    require_once('config.php');

    // Sender Info
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $message = trim($_POST['message']);
    $err = "";

    // Check Info
    $pattern = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$^";
    if(!preg_match_all($pattern, $email, $out)) {
        $err = MSG_INVALID_EMAIL; // Invalid email
    }
    if(!$email) {
        $err = MSG_INVALID_EMAIL; // No Email
    }   
    if(!$message) {
        $err = MSG_INVALID_MESSAGE; // No Message
    }
    if (!$name) {
        $err = MSG_INVALID_NAME; // No name 
    }

    //define the headers we want passed. Note that they are separated with \r\n
    $headers = "From: ".$name." <".$email.">\r\nReply-To: ".$email."";

    if (!$err){

        //send the email
        $sent = mail(TO_EMAIL,SUBJECT,$message,$headers); 

        if ($sent) {
                // If the message is sent successfully print
                echo "SEND"; 
            } else {
                // Display Error Message
                echo MSG_SEND_ERROR; 
            }
    } else {
        echo $err; // Display Error Message
    }
?>

HTML FORM -

  <!-- Contact Form -->
                <div class="contact-form">
                    <h3 class="main-heading"><span>Let's keep in touch</span></h3>
                    <div id="contact-status"></div>
                    <form action="" id="contactform">
                        <p>
                            <label for="name">Your Name</label>
                            <input type="text" name="name" class="input" >
                        </p>
                        <p>
                            <label for="email">Your Email</label>
                            <input type="text" name="email" class="input">
                        </p>
                        <p>
                            <label for="message">Your Message</label>
                            <textarea name="message" cols="88" rows="6" class="textarea" ></textarea>
                        </p>
                        <input type="submit" name="submit" value="Send your message" class="submit">
                    </form>

Solution

  • The problem with the form not processing correctly, is that you are using POST variables.

    Your form is missing post for its method. <form> defaults to GET if it's not specified.

    Change:

    <form action="" id="contactform">
    

    to

    <form action="" id="contactform" method="post">
    

    and it will work.