Gurgen Gurgen - 7 months ago 14
PHP Question

Failed to write a feedback form PHP

I need your help. I have a feedback form, which sends an email to a specified mailbox.
This is how my form looks like:

<form method=post name="feedback">
<div>
<div align="center">
<input class="textarea1" type="text" name="name" placeholder="Name" autocomplete="off">
<input class="textarea1" type="text" name="email" placeholder="Email" autocomplete="off">
<input class="textarea1" type="text" name="title" placeholder="Subject" autocomplete="off">
<textarea class="textarea" rows="10" name="mess" placeholder="Message" autocomplete="off"></textarea><br />
<input class="submit" type="submit" value="Send" name="submit"></div>
</form>
<img id="load" src="load.gif" style="display: none;" />
<div id="answer"></div>


This is how my script looks like in head of the html file:

<script type="text/javascript" src="jquery-2.1.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#feedback").submit(function(event) {
event.preventDefault();
$.ajax({
url: "feedback_lib.php",
beforeSend: function() {
$("#load").fadeIn(400);
},
type: "post",
data: $("#feedback").serialize(),
success: function(answer) {
$("#answer").html(answer);
}
}).done(function() {
$("#load").fadeOut(400);
});
});
});
</script>


And this is how my feedback_lib.php look like:

<?php

$email = $_POST["email"];
$mess = $_POST["mess"];
$title = $_POST["title"];
$name = $_POST["name"];
$result = 0;
define(EMAIL_TO, "myrealmail@mymail.com");

if ((strlen($email) < 5) or (strlen($message) < 10)) $result = 1; // длина сообщения
else {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$subject = "Письмо с Вашего сайта http://".$_SERVER["HTTP_HOST"]."/";
$header = "From <".$email.">\r\nContent-type: text/plain; charset=utf-8\r\n";
mail(EMAIL_TO, $subject, $mess, $title, $name);
$result = 3; //письмо отправлено
}
else $result = 2; // неправильный email
}
echo getAnswer($result);

function getAnswer($result = 0) {
switch ($result) {
case 0: $answer = "";
break;
case 1: $answer = "<p style='color: red;'>Հաղորդագրության նվազագույն երկարություն 10 նիշ, իսկ email-ինը` 5:</p>";
break;
case 2: $answer = "<p style='color: red;'>Սխալ email:</p>";
break;
case 3: $answer = "<p style='color: green;'>Հաղորդագրությունն ուղարկված է</p>";
}
return $answer;
}

?>


And my question is: why it doesn't work?
Where's my fault?
Thanks for answers.

Answer

Use feedback id in the form.

<form method=post name="feedback" id="feedback">
...........
...........
</form>