Simple PHP feedback form

Category:

The proposed feedback form sends the data entered by users to the server, from where they will be sent to the mailbox specified in the script.

Add the following code to the place on your page where you plan to place this form:

<?php
$c = rand(100,450);
$c2 = rand(1,50);
echo '<form method="POST" class="feedback">Send a message to the administration';
echo '<textarea name="message">'.$_SESSION["message"].'</textarea>';
echo '<div id="login">Your name: ';
echo '<input type="text" name="login" value="'.$_SESSION["login"].'"></div>';
echo '<div id="mail">E-mail: ';
echo '<input type="text" name="mail" value="'.$_SESSION["mail"].'"></div>';
echo '<div id="key">'.$c.' + '.$c2.' = ';
echo '<input type="hidden" name="key" value="'.md5($c+$c2).'">';
echo '<input type="text" name="summ_key" size="4"> ';
echo '<input type="submit" value="Send"></div></form>';

if ($_SESSION["notice"]!=''){
    echo $_SESSION["notice"];
    $_SESSION["notice"] = '';
}
?>

Add CSS styles:

<style type="text/css">
.feedback {
    width: 550px;
    margin: 15px auto;
    padding: 5px;
    text-align: center;
    background-color: #CCCCCC;
    border-radius: 7px;
    border: 1px solid #666666;
}
.feedback textarea { width: 540px; height: 80px; resize: none; }
.feedback>div { margin: 4px 0; }
.feedback input[type="text"] { border: 1px solid #666666; }
#login { float: left; }
#key, #mail { text-align: right; }
</style>

Now all that remains is to add to the very beginning of the page with the feedback form the code for processing the received message and sending it to the mail of the site administration:

<?php
if (session_id()=='') session_start();
if (isset($_POST["key"])) {
 $mail='admin@site.com';                //Your mailbox
 $redirect=$_SERVER['HTTP_REFERER'];
 $_SESSION["login"]=$_POST["login"];
 $_SESSION["mail"]=$_POST["mail"];
 $_SESSION["message"]=$_POST["message"];

 if ($_POST["key"]==md5($_POST["summ_key"])) {
    if ($_POST["login"]!='' and $_POST["message"]!='' and $_POST["mail"]!='') {
        $subject="Message via the feedback form";
        $msg='Имя: '.htmlspecialchars($_SESSION["login"]).PHP_EOL;
        $msg.='E-mail: '.htmlspecialchars($_SESSION["mail"]).PHP_EOL;
        $msg.=htmlspecialchars($_SESSION["message"]);

        $headers="MIME-Version: 1.0\r\n";
        $headers.="Content-type: text/plain; charset=utf-8\r\n";
        $headers.="From: site.com";  //  your website address
        mail($mail, $subject, $msg, $headers);
        $_SESSION["notice"]='Your message has been sent!';
        $_SESSION["login"]='';
        $_SESSION["message"]='';
        $_SESSION["mail"]='';
        header("Location: $redirect");
        exit;
    }
    else $_SESSION["notice"]="You have not filled in all the fields!";
 }
 else {
    $_SESSION["notice"]="Incorrect verification code!";
    header("Location: $redirect");
    exit;
 }
}
?>

If you copied everything and placed it on your site correctly, then you will have your own feedback form as in the example. And don’t forget to correctly specify your mailbox in the $mail variable.