Contact form with html and php

236 views Asked by At
Hi im a newbie so i tried to use some source code to create a contact form for my website it does not work so i need help here is my code: 

HTML

    <div class="col-md-6 col-sm-6">
        <div class="row contact-form">
            <form id="contact-form" action="php/mail.php">
                <fieldset class="col-md-6 col-sm-6">
                    <input id="name" type="text" name="name" placeholder="Name">
                </fieldset>
                <fieldset class="col-md-6 col-sm-6">
                    <input type="email" name="email" id="email" placeholder="Email">
                </fieldset>
                <fieldset class="col-md-12">
                    <input type="text" name="subject" id="subject" placeholder="Subject">
                </fieldset>
                <fieldset class="col-md-12">
                    <textarea name="comments" id="comments" placeholder="Message"></textarea>
                </fieldset>
                <fieldset class="col-md-12">
                    <input type="submit" name="send" value="Send Message" id="submit" class="button">
                </fieldset>
            </form> 
        </div> <!-- /.contact-form -->   
    </div> <!-- /.col-md-6 -->

PHP

    <?php
    include 'functions.php';

    if (!empty($_POST)){
        $data['success'] = true;
        $_POST = multiDimensionalArrayMap('cleanEvilTags', $_POST);
        $_POST = multiDimensionalArrayMap('cleanData', $_POST);

        // your email adress 
        $emailTo ="[email protected]"; // "[email protected]";

        // from email adress
        $emailFrom ="[email protected]"; // "[email protected]";

        // email subject
        $emailSubject = "Mail from Web Contact Form ";

        $name = $_POST["name"];
        $email = $_POST["email"];
        $comment = $_POST["comment"];
        if($name == "")
            $data['success'] = false;

        if (!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)) 
            $data['success'] = false;


        if($comment == "")
            $data['success'] = false;

        if($data['success'] == true){
            $message = "NAME: $name<br>
                EMAIL: $email<br>
                COMMENT: $comment";

            $headers = "MIME-Version: 1.0" . "\r\n"; 
            $headers .= "Content-type:text/html; charset=utf-8" . "\r\n"; 
            $headers .= "From: <$emailFrom>" . "\r\n";
            mail($emailTo, $emailSubject, $message, $headers);

            $data['success'] = true;
            echo json_encode($data);
        }
    }

Thank you. I changed the code to this :

<?php

include 'functions.php';

if (!empty($_POST)){

  $data['success'] = true;


  //your email adress 
  $emailTo ="[email protected]"; //"[email protected]";

  //from email adress
  $emailFrom = $_POST["email"]; //"[email protected]";

  //email subject
  $emailSubject = $_POST["subject"];

  $name = $_POST["name"];
  $email = $_POST["email"];
  $comments = $_POST["comments"];
  if($name == "")
   $data['success'] = false;

 if (!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)) 
   $data['success'] = false;


 if($comments == "")
   $data['success'] = false;

 if($data['success'] == true){

  $message = "NAME: $name<br>
  EMAIL: $email<br>
  COMMENT: $comments";


  $headers = "MIME-Version: 1.0" . "\r\n"; 
  $headers .= "Content-type:text/html; charset=utf-8" . "\r\n"; 
  $headers .= "From: <$emailFrom>" . "\r\n";
  mail($emailTo, $emailSubject, $message, $headers);

  $data['success'] = true;
  echo json_encode($data);
}`enter code here`
}

I would also like to know if i should use a php_redirect to get the browser to open the html file not the php file

The website is abvconstruction.co.za. If any one can check what is the error on my code

2

There are 2 answers

2
TheBalco On

First, you have to add method="post" to your <form>, otherwise it's sending the data with GET.

Also remove these lines, which clear the POST data:

$_POST  = multiDimensionalArrayMap('cleanEvilTags', $_POST);
$_POST  = multiDimensionalArrayMap('cleanData', $_POST);
0
Grumpy On

first remove these line, they clear the post values

  $_POST  = multiDimensionalArrayMap('cleanEvilTags', $_POST);
  $_POST  = multiDimensionalArrayMap('cleanData', $_POST);