Loading Searchbox
9lessons programming blog logo
Friday, May 1, 2009

Insert and Load Record using jQuery and Ajax.

36 comments
This tutorials explains how to insert and display record without refreshing web page. I had developed using jQuery and Ajax it's simple and useful take a look at this post.

Next Part
Slide down effect.

Insert and  Load Record using jQuery and Ajax.

Download Script     Live Demo

Database Design
My previous post : Twitter Like More Button with jQuery and Ajax.

inserting.php
Contains of javascript and HTML code.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js
">
</script>
<script type="text/javascript" >
$(function() {
$(".comment_button").click(function() {

var test = $("#content").val();
var dataString = 'content='+ test;

if(test=='')
{
alert("Please Enter Some Text");
}
else
{
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="ajax-loader.gif" align="absmiddle"> <span class="loading">Loading Comment...</span>');

$.ajax({
type: "POST",
url: "demo_insert.php",
data: dataString,
cache: false,
success: function(html){
$("#display").after(html);
document.getElementById('content').value='';
document.getElementById('content').focus();
$("#flash").hide();
}
});
} return false;
});
});
</script>
// HTML code
<div>
<form method="post" name="form" action="">
<h3>What are you doing?</h3>
<textarea cols="30" rows="2" name="content" id="content" maxlength="145" >
</textarea><br />
<input type="submit" value="Update" name="submit" class="comment_button"/>
</form>
</div>
<div id="flash"></div>
<div id="display"></div>



demo_insert.php
PHP Code display recently inserted record from the database.
<?php
include('db.php');
if(isSet($_POST['content']))
{
$content=$_POST['content'];
mysql_query("insert into messages(msg) values ('$content')");
$sql_in= mysql_query("SELECT msg,msg_id FROM messages order by msg_id desc");
$r=mysql_fetch_array($sql_in);
}
?>
<b><?php echo $r['msg']; ?></b>


Tutorials list

9lessos Table of Contents

Feel free post a comment. Please don't spam

Next Part
Insert a Record with animation slide down effect using jQuery and Ajax.
Sponsored Links

Recent Posts

Share this post

Subscribe to my feeds

Subscribe
Comments
36 comments
Prashant Karkera said...

Good one...

Anonymous said...

Is it spam proof?

Anonymous said...

nice one... but please... remove the layout tables. we live in 2009

Srinivas Tamada said...

removed layout tables

Anonymous said...

why use $("#flash").show();
$("#flash").fadeIn(400).html('img src="ajax-loader.gif" align="absmiddle"> span class="loading">Loading Comment...span>');??

jquery.ajax has the option: beforeSend use that insteed

Vivekanand said...

Thanks for this script and thanks for your effort too... :)

Thanks,
Vivek
[http://www.developersnippets.com]

Anonymous said...

good script but your shirt is better.

Anonymous said...

thanks from http://www.bugzlife.com

Anonymous said...

thanks

Anonymous said...

nice nice

Generaccion said...

really good good i seach this tutorial anytime

Anonymous said...

how do i limit the displayed comment?

Anonymous said...

Nice script

Anonymous said...

good script, but how can I load old messages first and use a button to delete the message with the same script

Ash said...

Nice script, but open to SQL injection.

Anonymous said...

thanks... it helps me a lot

ile said...

Thanks for script, it's really great and simple. I'm using it for tags... it would be great if there would be solution to reverse display order of submitted content.
This is the part to be edited but I cant figure out how to solve it:
success: function(html){
$("#display").after(html);
}

Ohm said...

Thanks.

Anonymous said...

great

Anonymous said...

nice

Anonymous said...

good post

Anonymous said...

how to create share any links like facebook using jquery

Anonymous said...

interesting

Anonymous said...

thanks, excellent script, just what i was looking for!!

Anonymous said...

Is it possible to pass POST[] array to jquery insert.?

Anonymous said...

nice

sanza702 said...

Brilliant, it really made things come to lite, for a whole day I was struggling with something, but this... Awesome mate, keep it up.

Anonymous said...

thanks for the script...:)

Mehmet said...

When adding more than one record problem is the problem can you help when you add?

Bill Jay said...

Is there a way to make this code reusable? (i.e several comment boxes on the same page)

Anonymous said...

thanx

Anonymous said...

very good! :)

Anonymous said...

Good

vicky said...

very nice post but doesnt work in ie 6 version pls help ????

baambootratu said...

good

baambootratu said...

good

Post a Comment

Orkut | FacebookAbout Me

Subscribe now!Feeds RSS

Subscribe now!Recent Posts

Subscribe now!Categories

Subscribe now!Comments

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine

Join into my community

Labs ProfileRelease

My ProfileTwitter