Contact Form HTML + PHP

Asked

Viewed 299 times

0

Someone helps me to solve this small problem, I have a Contact Form to send messages through the web-site, I have tried to configure anyway but keeps giving the error "Message not sent".

HTML code:

<form id="contact-form">
                                        <span class="wpcf7-form-control-wrap your-name">
                                             <input type="text"  id="name" name="name" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" placeholder="Nome completo"/>
                                        </span>
                                        <span class="wpcf7-form-control-wrap your-email">
                                            <input type="text" name="mail" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email" aria-required="true" aria-invalid="false" placeholder="Email"/></span>
                                        <span class="wpcf7-form-control-wrap your-subject">
                                            <input type="text" name="website"  value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" placeholder="Assunto"/></span>
                                        <p>
                                            <span class="wpcf7-form-control-wrap your-message">
                                                <textarea  name="comment" id="comment" cols="40" rows="10" class="wpcf7-form-control wpcf7-textarea" aria-invalid="false" placeholder="Escreva a mensagem aqui..."></textarea></span>
                                        </p>
                                        <p>
                                            <input type="submit" id="submit_contact" value="Enviar" class="wpcf7-form-control wpcf7-submit"/>
                                            <div id="msg" class="message"></div>
                                        <p></p>

                                    </form>

PHP code:

<?php 

/* ==========================  Define variables ========================== */

#Your e-mail address
define("__TO__", "[email protected]");

#Message subject
define("__SUBJECT__", "");

#Success message
define('__SUCCESS_MESSAGE__', "A sua mensagem foi enviada. Obrigado!");

#Error message 
define('__ERROR_MESSAGE__', "Erro, a sua mensagem não foi enviada");

#Messege when one or more fields are empty
define('__MESSAGE_EMPTY_FILDS__', "Por favor preencha todos os campos");

/* ========================  End Define variables ======================== */

//Send mail function
function send_mail($to,$subject,$message,$headers){
    if(@mail($to,$subject,$message,$headers)){
        echo json_encode(array('info' => 'success', 'msg' => __SUCCESS_MESSAGE__));
    } else {
        echo json_encode(array('info' => 'error', 'msg' => __ERROR_MESSAGE__));
    }
}

//Check e-mail validation
function check_email($email){
    if(!@eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
        return false;
    } else {
        return true;
    }
}

//Get post data
if(isset($_POST['name']) and isset($_POST['mail']) and isset($_POST['comment'])){
    $name    = $_POST['name'];
    $mail    = $_POST['mail'];
    $website  = $_POST['website'];
    $comment = $_POST['comment'];

    if($name == '') {
        echo json_encode(array('info' => 'error', 'msg' => "Por favor insira o seu nome."));
        exit();
    } else if($mail == '' or check_email($mail) == false){
        echo json_encode(array('info' => 'error', 'msg' => "Por favor insira um e-mail valido."));
        exit();
    } else if($comment == ''){
        echo json_encode(array('info' => 'error', 'msg' => "Por favor insira a sua mensagem."));
        exit();
    } else {
        //Send Mail
        $to = __TO__;
        $subject = __SUBJECT__ . ' ' . $name;
        $message = '
        <html>
        <head>
          <title>Mensagem de '. $name .'</title>
        </head>
        <body>
          <table class="table">
            <tr>
              <th align="right">Nome:</th>
              <td align="left">'. $name .'</td>
            </tr>
            <tr>
              <th align="right">E-mail:</th>
              <td align="left">'. $mail .'</td>
            </tr>
            <tr>
              <th align="right">Assunto:</th>
              <td align="left">'. $subject .'</td>
            </tr>
            <tr>
              <th align="right">Mensagem:</th>
              <td align="left">'. $comment .'</td>
            </tr>
          </table>
        </body>
        </html>
        ';

        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
        $headers .= 'From: ' . $mail . "\r\n";

        send_mail($to,$subject,$message,$headers);
    }
} else {
    echo json_encode(array('info' => 'error', 'msg' => __MESSAGE_EMPTY_FILDS__));
}

?>

  • on the PHP side he’s recognizing $_POST? in your form you don’t have method="POST"...

1 answer

1


What happens that besides not having the method='post' you are not giving action in sending.

<input type="submit" id="submit_contact" value="Enviar" class="wpcf7-form-control wpcf7-submit"/>

You can put

<input type="submit" id="submit_contact" value="Enviar" name="enviar" class="wpcf7-form-control wpcf7-submit"/>

then do the following in php

//Get post data
if(isset($_POST['enviar'])){
    $name    = $_POST['name'];
    $mail    = $_POST['mail'];
    $website  = $_POST['website'];
    $comment = $_POST['comment'];

and in the form

<form action="" method="POST" enctype="multipart/form-data">
 <span class="wpcf7-form-control-wrap your-name">
<input type="text"  id="name" name="name" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" placeholder="Nome completo"/></span>
<span class="wpcf7-form-control-wrap your-email">
<input type="text" name="mail" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email" aria-required="true" aria-invalid="false" placeholder="Email"/></span>
<span class="wpcf7-form-control-wrap your-subject">
<input type="text" name="website"  value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" placeholder="Assunto"/></span>
<p>
<span class="wpcf7-form-control-wrap your-message">
<textarea  name="comment" id="comment" cols="40" rows="10" class="wpcf7-form-control wpcf7-textarea" aria-invalid="false" placeholder="Escreva a mensagem aqui..."></textarea></span>
</p>
<p>
<input type="submit" id="submit_contact" value="Enviar" name="enviar" class="wpcf7-form-control wpcf7-submit"/>
<div id="msg" class="message"></div>
<p></p>
 </form>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.