JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://static.yanke.ru/js/jquery.form.validation.js"></script>
<form id="form" method="post" action="javascript:void(0);" onsubmit="ajax()">
<p>Ваше имя</p>
<p>
<input type="text" name="name" class="name" value="" />
</p>
<p>Ваш email</p>
<p>
<input type="text" name="email" class="email" value="" />
</p>
<p>Тема</p>
<p>
<input type="text" name="subject" class="subject" value="" />
</p>
<p>Сообщение</p>
<p>
<textarea name="message" class="message"></textarea>
</p>
<p>
<input type="submit" name="send" class="send" value="send">
</p>
</form>
<div id="results"></div>Наш php код:
<pre>
<?php
if (!isset($_POST['name']) or empty($_POST['name'])) {
$error1 = "Имя?<br />";
} else $error1 = NULL;
if (!isset($_POST['email']) or empty($_POST['email'])) {
$error2 = "Email?<br />";
} else $error2 = NULL;
if (!isset($_POST['subject']) or empty($_POST['subject'])) {
$error3 = "Тема?<br />";
} else $error3 = NULL;
if (!isset($_POST['message']) or empty($_POST['message'])) {
$error4 = "Сообщение?<br />";
} else $error4 = NULL;
if (empty($error1) and empty($error2) and empty($error3) and empty($error4)) {
$subject = $_POST['subject'];
$name = $_POST['name'];
$email = $_POST['email'];
$message = "Имя: {$name}, email: {$email}, сообщение: {$_POST['message']}";
if (mail("zombiqwerty@yandex.ru", $subject, $message)) {
echo "Отправлено!";
} else echo "Ошибка!";
} else {
echo $error1.$error2.$error3.$error4;
}
?>
</pre>
CSS
input {
outline:none;
}
.invalid {
background: rgba(255, 0, 0, 0.4);
}
input, textarea {
width: 200px;
}
.error {
position: absolute;
width: 200px;
height: 30px;
text-align: center;
line-height: 30px;
margin-left: 10px;
display: inline-block;
vertical-align: top;
background: #ebf5ff;
border: 1px solid #b1d3f5;
}
.error::before {
content: url(http://zombiqwerty.ru/files/corner.gif);
width: 13px;
height: 6px;
position: absolute;
top: 4px;
left: -10px;
}
JavaScript
function ajax() { //Ajax отправка формы
var msg = $("#form").serialize();
$.ajax({
type: "POST",
url: "",
data: msg,
success: function (data) {
$("#results").html(data);
},
error: function (xhr, str) {
alert("Возникла ошибка!");
}
});
}
jQuery.fn.notExists = function () { //Проверка на существование элемента
return $(this).length == 0;
}
$(document).ready(function () { //Валидация формы
$(".send").validation(
$(".name").validate({
test: "blank letters",
invalid: function () {
if ($(this).nextAll(".error").notExists()) {
$(this).after('<div class="error">Введите корректное имя</div>');
$(this).nextAll(".error").delay(2000).fadeOut("slow");
setTimeout(function () {
$(".name").next(".error").remove();
}, 2600);
}
},
valid: function () {
$(this).nextAll(".error").remove();
}
}),
$(".email").validate({
test: "blank email",
invalid: function () {
if ($(this).nextAll(".error").notExists()) {
$(this).after('<div class="error">Введите корректный email</div>');
$(this).nextAll(".error").delay(2000).fadeOut("slow");
setTimeout(function () {
$(".email").next(".error").remove();
}, 2600);
}
},
valid: function () {
$(this).nextAll(".error").remove();
}
}),
$(".subject").validate({
test: "blank",
invalid: function () {
if ($(this).nextAll(".error").notExists()) {
$(this).after('<div class="error">Введите тему</div>');
$(this).nextAll(".error").delay(2000).fadeOut("slow");
setTimeout(function () {
$(".subject").next(".error").remove();
...