Ads

May 12, 2013

Comment system with jQuery, Ajax and PHP


This time I had developed Comment system with out refreshing page with jQuery, Ajax and PHP. In this application I had implemented with gravatar image. It's is useful and simple just some lines of code. Take a look at the live demo effect.


Updated Version :Click Here


Download Script     Live Demo


javascript code.

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" >
$(function() {
$(".submit").click(function()
{
var name = $("#name").val();
var email = $("#email").val();
var comment = $("#comment").val();
var dataString = 'name='+ name + '&email=' + email + '&comment=' + comment;
if(name=='' || email=='' || comment=='')
{
alert('Please Give Valid Details');
}
else
{
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="ajax-loader.gif" />Loading Comment...');
$.ajax({
type: "POST",
url: "commentajax.php",
data: dataString,
cache: false,
success: function(html){
$("ol#update").append(html);
$("ol#update li:last").fadeIn("slow");
$("#flash").hide();
}
});
}return false;
}); });
</script>



comment.php
Contains HTML code here class timeline li{display:none}
<ol id="update" class="timeline">
</ol>
<div id="flash"></div>
<div >
<form action="#" method="post">
<input type="text" id="name"/>Name<br />
<input type="text" id="email"/>Email<br />
<textarea id="comment"></textarea><br />
<input type="submit" class="submit" value=" Submit Comment " />
</form>
</div>

commentajax.php
Contains PHP and HTML code.
<?php
if($_POST)
{
$name=$_POST['name'];
$email=$_POST['email'];
$comment=$_POST['comment'];
$lowercase = strtolower($email);
$image = md5( $lowercase );
mysql_query("SQL Comment table insert statement");
}
else { }
?>

<li class="box">
<img src="http://www.gravatar.com/avatar.php?gravatar_id=
<?php echo $image; ?>"/>
<?php echo $name;?><br />
<?php echo $comment; ?>
</li>

More jQuery and Ajax Tutorials : Take a look at this link

3 comments:

Anonymous said...

nice concept

ipodnerd said...

I love the concept of posting to a page and using ajax to display that page!

Anonymous said...

its very good

Most Popular Posts