index.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Скрипт ajax коментариев</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#validate").keyup(function () {
var email = $("#validate").val();
if (email != 0) {
if (isValidEmailAddress(email)) {
$("#validEmail").css({
"background-image": "url('validYes.png')"
});
} else {
$("#validEmail").css({
"background-image": "url('validNo.png')"
});
}
} else {
$("#validEmail").css({
"background-image": "none"
});
}
});
});
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(
/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i
);
return pattern.test(emailAddress);
}
</script>
<script type="text/javascript">
$(function () {
$(".submit").click(function () {
var name = $("#name").val();
var email = $("#email").val();
var comment = $("#comment").val();
var post_id = $("#post_id").val();
var dataString = 'name=' + name + '&email=' + email + '&comment=' + comment + '&post_id=' +
post_id;
if (name == '') {
alert('Вы не указали своё имя!');
}
if (email == '') {
alert('Вы не указали свой email!');
}
if (comment == '') {
alert('Вы не написали комментарий!');
}
if (name == '' || email == '' || comment == '') {
alert('Заполните все поля формы!');
} else {
$("#flash").show();
$("#flash").fadeIn(400).html(
'<img src="ajax-loader.gif" align="absmiddle"> <span class="loading">Loading Comment...</span>'
);
$.ajax({
type: "POST",
url: "ajaxcomment.php",
data: dataString,
cache: false,
success: function (html) {
$("ol#update").append(html);
$("ol#update li:last").fadeIn("slow");
document.getElementById('validate').value = '';
document.getElementById('name').value = '';
document.getElementById('comment').value = '';
$("#name").focus();
$("#flash").hide();
}
});
}
return false;
});
});
</script>
<style type="text/css">
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
}
.comment_box {
background-color: #D3E7F5;
border-bottom: #ffffff solid 1px;
padding-top: 3px
}
a {
text-decoration: none;
color: #d02b55;
}
a:hover {
text-decoration: underline;
color: #d02b55;
}
* {
margin: 0;
padding: 0;
}
ol.timeline {
list-style: none;
font-size: 1.0em;
}
ol.timeline li {
position: relative;
padding: .05em 0 .6em 0;
}
ol.timeline li:first-child {}
#main {
width: 500px;
margin-top: 20px;
margin-left: 100px;
font-family: "Trebuchet MS";
}
#flash {
margin-left: 100px;
}
.box {
height: 85px;
border: #dedede dashed 1px;
margin-bottom: 5px;
font-family: "Trebuchet MS";
font-size: 12px;
color: #686868;
}
input {
color: #000000;
font-size: 14px;
border: #cccccc solid 1px;
height: 18px;
margin-bottom: 10px;
width: 200px;
}
textarea {
color: #000000;
font-family: "Trebuchet MS";
font-size: 12px;
border: #cccccc solid 1px;
height: 104px;
margin-bottom: 10px;
width: 280px;
}
.titles {
font-size: 13px;
}
.star {
color: #FF0000;
font-size: 16px;
font-weight: bold;
padding-left: 5px;
}
.com_img {
float: left;
width: 80px;
height: 80px;
margin-right: 20px;
}
.com_name {
font-size: 13px;
color: rgb(255, 64, 0);
font-weight: bold;
}
#validEmail {
margin-top: 4px;
margin-left: 9px;
position: absolute;
width: 16px;
height: 16px;
}
.text {
font-family: Arial, Tahoma, Helvetica;
}
</style>
</head>
<body>
<div id="main">
<ol id="update" class="timeline">
<?php
// $post_id value comes from the POSTS table
@$path=dirname(__FILE__);
$comments_name = $path."comments/name_doc.cmts";
if(file_exists($comments_name)){
$comments=file($comments_name);
$count_comments = count($comments);
for($i=0;$i < $count_comments;$i++) {
$comm=unserialize($comments[$i]);
if ($comm=="") {continue;}
@$comm_markers=array(
@$pubdate = $comm['pubdate'],
@$pubtime = $comm['pubtime'],
@$name = $comm['name'],
@$title_name = $comm['title'],
@$email = $comm['email'],
@$comment_dis = $comm['comment']
);
$lowercase = strtolower($email);
$image = md5($lowercase);
?>
<li class="box">
<img src="default.png" class="com_img">
<span class="com_name"> <a href="mailto:<?php echo $email; ?>"><?php echo $name; ?></a></span>
<div style="font-size:10px; color:#B8B8B8; float: right; margin-right:5px "><?php echo $pubdate; ?>,
<?php echo $pubtime; ?></div><br />
<?php echo $comment_dis; ?>
</li>
<?php } } ?>
</ol>
<div id="flash"></div>
<div style="margin-left:100px">
<form action="#" method="post">
<span class="titles">Ваше имя</span><span class="star">*</span> <input type="text" name="title"
id="name" /><br />
<span class="titles">Ваш Email</span><span class="star">*</span> <input type="text" name="email"
id="validate" width="30"><span id="validEmail"></span><br />
<textarea name="comment" id="comment"></textarea><br />
<input type="submit" class="submit" value=" Добавить комментарий " />
</form>
</div>
</div>
</body>
</html>
ajaxcomment.php
<?php
@$path=dirname(__FILE__).//";
$dbcomments_name = $path."comments/name_doc.cmts";
function savedata ($filename, $data, $rez="a+"){
@chmod($filename, 0777);
$f=fopen("$filename", $rez) or die("can't open file");
flock($f,LOCK_EX);
fwrite($f,serialize($data)."\n");
flock($f,LOCK_UN);
fclose($f);
@chmod ("$filename",0664);
}
$date=date("d.m.Y");
$time=date("H:i:s");
$rez="a+";
if (isset($_POST['submitBtn'])){
$secCode = isset($_POST['secCode']) ? strtolower($_POST['secCode']) : "";
if ($secCode == $_SESSION['securityCode']) {
echo "<p>The result code was valid!<br/></p>";
echo "<p><a href=\"".$_SERVER['PHP_SELF']."\">Repeat the test!</a><br/><br/></p>";
unset($_SESSION['securityCode']);
$result = true;
}
else {
echo "<p>Sorry the security code is invalid! Please try it again!</p>";
$result = false;
}
}
if($_POST)
{
$name=$_POST['name'];
$email=$_POST['email'];
$comment_dis=$_POST['comment'];
$data=array("pubdate"=>$date, "pubtime"=>$time, "name"=>$name,"email"=>$email, "comment"=>$comment_dis);
savedata("$dbcomments_name", $data, "$rez");
}
?>
<li class="box">
<img src="default.png" class="com_img">
<span class="com_name"> <a href="<?php echo $email; ?>"><?php echo $name; ?></a></span> <div style="font-size:10px; color:#B8B8B8; float: right; margin-right:5px "><?php echo $date; ?>, <?php echo $time; ?></div><br />
<?php echo @$comment_dis; ?>
</li>