Jaimee Williams
Jaimee Williams

Reputation: 1

My PHP Contact form is not working

I'm having a problem with my contact form and I don't know if it is the contact form that is a problem or the PHP, can anyone look at it and tell me what's wrong?Like I said, I have looked it over and over to make it to see what the problem

<form>
    <div class="row">
        <div class="col-md-6">
            <div class="group">
                <input id="name" type="text"><span class="highlight"></span><span class="bar"></span>
                <label>Name</label>
            </div>
        </div>
        <div class="col-md-6">
            <div class="group">
                <input id="email" type="email"><span class="highlight"></span><span class="bar"></span>
                <label>Email</label>
            </div>
        </div>
    </div>
    <div class="group">
        <textarea class="form-control" rows="5" id="messege"></textarea>
        <span class="highlight"></span><span class="bar"></span>
        <label>Your Messege</label>
    </div>
    <button class="ripple">Send</button>
</form>

Here is the PHP Part;

<?php
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['subject']) && isset($_POST['message']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {

  // detect & prevent header injections
  $test = "/(content-type|bcc:|cc:|to:)/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $test, $val ) ) {
      exit;
    }
  }

$headers = 'From: ' . $_POST["name"] . '<' . $_POST["email"] . '>' . "\r\n" .
    'Reply-To: ' . $_POST["name"] . '<' . $_POST["email"] . '>' . "\r\n" .
    'Return-Path: ' . $_POST["name"] . '<' . $_POST["email"] . '>' . "\r\n" .
    'MIME-Version: 1.0' . "\r\n" .
    'Content-Type: text/html; charset=utf-8' . "\r\n" .
    'X-Priority: 1' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

  //  Replace with your email
  mail( "[email protected]", $_POST['subject'], $_POST['message'], $headers );

}
?>

Upvotes: 0

Views: 96

Answers (2)

Dhara Vihol
Dhara Vihol

Reputation: 612

You have added 'id' in form, but you can not access the input value by the use of id, instead you have to use 'name' attribute. You also have to give action and method attribute in the form tag. so that the input values will be send to the selected path or file by the method you have choosen. I have updated your code with solution. You should try this.

<form method="post" action="filename or path">
    <div class="row">
        <div class="col-md-6">
            <div class="group">
                <input id="name" name="name" type="text"><span class="highlight"></span><span class="bar"></span>
                <label>Name</label>
            </div>
        </div>
        <div class="col-md-6">
            <div class="group">
                <input id="email" name="email" type="email"><span class="highlight"></span><span class="bar"></span>
                <label>Email</label>
            </div>
        </div>
    </div>
    <div class="group">
        <textarea class="form-control" rows="5" name="message" id="messege"></textarea>
        <span class="highlight"></span><span class="bar"></span>
        <label>Your Messege</label>
    </div>
    <input type="button" name="button" class="ripple" value="send">
</form>

But if you have written your php code in the same file, than you do not need to add method and action attribute in the <form>. and you should add the name attribute in the <button>, so that you can assure that the php code will be executed if the button is clicked.

<button name="submit" class="ripple">Send</button>

and update your php code with this:

<?php
if(isset($_POST['submit']))
{
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['subject']) && isset($_POST['message']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {

  // detect & prevent header injections
  $test = "/(content-type|bcc:|cc:|to:)/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $test, $val ) ) {
      exit;
    }
  }

$headers = 'From: ' . $_POST["name"] . '<' . $_POST["email"] . '>' . "\r\n" .
    'Reply-To: ' . $_POST["name"] . '<' . $_POST["email"] . '>' . "\r\n" .
    'Return-Path: ' . $_POST["name"] . '<' . $_POST["email"] . '>' . "\r\n" .
    'MIME-Version: 1.0' . "\r\n" .
    'Content-Type: text/html; charset=utf-8' . "\r\n" .
    'X-Priority: 1' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

  //  Replace with your email
  mail( "[email protected]", $_POST['subject'], $_POST['message'], $headers );  
}
else
{
   //echo error
}
?>

Upvotes: 1

Marc B
Marc B

Reputation: 360882

You have no name attributes on your form fields:

<input id="name" type="text"><span class="hi [..snip..]

id is NOT used for form submissions. No names, no fields submitted.

Even some BASIC debugging, like var_dump($_POST) would have shown you the problem.

Upvotes: 1

Related Questions