I love jQuery framework, feels like we can do some awesome things easily. If you are familiar with the basic of jQuery framework too, the next thing you have to do is learn to use jQuery Ajax to add and retrieve records from MySql database. Let’s take a look at how we implement real time add and retrieve records using Ajax.
Database
sample database comment table columns id, name and message
+---------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+----------------+ | id | int(5) | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | message | varchar(120) | NO | | NULL | | +---------+--------------+------+-----+---------+----------------+
comment.php
<html> <head> <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script> <script type="text/javascript" src="js/jquery-ui-1.8.17.custom.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#button").click(function(){ var name=$("#name").val(); var message=$("#message").val(); $.ajax({ type:"post", url:"process.php", data:"name="+name+"&message="+message, success:function(data){ $("#info").html(data); } }); }); }); </script> </head> <body> <form> name : <input type="text" name="name" id="name"/> </br> message : <input type="text" name="message" id="message" /> </br> <input type="button" value="Send Comment" id="button"> <div id="info" /> </form> </body> </html>
process.php
<?php mysql_connect("localhost","root",""); mysql_select_db("php_databaseajax"); $name=$_POST["name"]; $message=$_POST["message"]; $query=mysql_query("INSERT INTO comment(name,message) values('$name','$message') "); if($query){ echo "Your comment has been sent"; } else{ echo "Error in sending your comment"; } ?>
Try to input some comments using that form, here’s what i got
The record i just submitted is successfully added to the database. Although we can’t see right now but it can be checked directly in your MySql.
+----+----------------+---------------------------+ | id | name | message | +----+----------------+---------------------------+ | 1 | Agung Setiawan | Cool comment | | 2 | Nusantara | Indonesia is hot but nice | +----+----------------+---------------------------+
What is the value of the comment feature of web app if visitors can’t see their own comment. Now, this is our time to be a hero… 😀
Edit both files that we have created to add the ability to retrieve records from database.
comment.php
<html> <head> <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script> <script type="text/javascript" src="js/jquery-ui-1.8.17.custom.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ function showComment(){ $.ajax({ type:"post", url:"process.php", data:"action=showcomment", success:function(data){ $("#comment").html(data); } }); } showComment(); $("#button").click(function(){ var name=$("#name").val(); var message=$("#message").val(); $.ajax({ type:"post", url:"process.php", data:"name="+name+"&message="+message+"&action=addcomment", success:function(data){ showComment(); } }); }); }); </script> </head> <body> <form> name : <input type="text" name="name" id="name"/> </br> message : <input type="text" name="message" id="message" /> </br> <input type="button" value="Send Comment" id="button"> <div id="info" /> <ul id="comment"></ul> </form> </body> </html>
process.php
<?php mysql_connect("localhost","root",""); mysql_select_db("php_databaseajax"); $action=$_POST["action"]; if($action=="showcomment"){ $show=mysql_query("Select * from comment order by id desc"); while($row=mysql_fetch_array($show)){ echo "<li><b>$row[name]</b> : $row[message]</li>"; } } else if($action=="addcomment"){ $name=$_POST["name"]; $message=$_POST["message"]; $query=mysql_query("INSERT INTO comment(name,message) values('$name','$message') "); if($query){ echo "Your comment has been sent"; } else{ echo "Error in sending your comment"; } } ?>
Now it is more cool with visitor’s comment directly show up after Send Button is clicked
Hi,
That is very neat and just what I was looking for
Quick question. If i have several topics on a page and hence different add comment boxes for each topic, how would i change the code?
Best,
Rob
Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something.
I think that you can do with some pics to drive the
message home a bit, but other than that, this is excellent
blog. A fantastic read. I will certainly be back.
Struggling to get your car paid off? Are you mad about the
last deal you got? Perhaps you’re looking for an automobile now, and you’re not sure what to
do differently. You’re in the same boat with many other people. Keep reading to find out information regarding what to do next time you enter a dealership.
Thank you for the auspicious writeup. It in fact was a amusement account it.
Look advanced to more added agreeable from you! However,
how could we communicate?
The simplest tutorial by far. Might just add-up, I wanted to auto refresh div every X seconds, would you advise to execute a db query or use rss as source feed.
TIA.
nice toturial
Thanks gay that’s was very helpful for me. you the best…..
Phenomenal! That was helpful!!
Short and nice one!!! Keep up the good work
It is really Simple and easy to understand.. I really got benefited from this.
Thanks a lot
This tutorial was a great help! Thanks!
Am I the only one who gets this problem?
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /var/www/[mrURL]/process.php on line 11
how to upload a file using same script please define it.
how to fetch data one by one and selecting at at particular option then it will show wrong answer or right answer,so plz help me to try this problem and also to use different id
tst
Thanks for that great tutorial.
clean nd easy code thanks
i want to try it man
not worked
You rocks…thanks a lot for a wonderful article.
i used your first part of tutorial to insert into database but everything except date is not getting inserted to database.Is it because i am using a jquery based calender in my form?please help me out i am new to ajax
Thank you for simple but powerful example.
Its really help…………………………………………..for beginners who learn frameworks…….
hii
wowwww
Thanks for that great tutorial easy and simple
Thanks a lot, its very helpful
This is called a Tutorial, Thanks! Keep on Working!
Thank you very much, Your explained it beautifully
nice web design
Thanks for this tutorial. I’m definitely bookmaking this site
I would’ve loved to download the source code though. Nonetheless copy and paste can always do the job for me. Thanks (-:
Teşekkürler güzel bilgiler paylaşmışsın
Reblogged this on Bundle of Articles.
Very useful example, thx!
osm frame work
easily understand this example thank u
Hi this session is helpful. but still I want to do something different if you can help me
If I want to do like “Select * from comment where id=?” then how to do it using JQuery and Ajax.
awesome explanation i try to do this but having a lot of bug to send a data string on ajax file finally i have see this article i got a solicitation its working thanks for sharing this article
alert(hello)
fetch data from mysql database without click on any button…. ( like chat box of facebook where we type text after enter we see result )
I didnt read all the post but i scrolled to bottom and and top like 5 times just to see the fish animation. This really won my heart. Amazing!
🙂
You can show / teach me how it is made. I have given you my Email address.
This code is quite and simple and it is well explained.
Anyone can get it easily by implementing it .
while i m fetching data using ajax, in that data i have a button which not working while i m trying to call a javascript function.
Thanks bhai
that’s lot of help thank you
Fantastic Info! But I???¨º?¨¨m having some trouble trying to load your blog. I have read it many times just before and never gotten a thing like this, but now when I attempt to load some thing it just takes a bit whilst (5-10 minutes ) after which just stops. I hope i don???¨º?¨¨t have spyware or a thing. Does anyone know what the difficulty might be?
Boite communication Skikda http://www.foodspotting.com/5001066
Can you tell us more about that please ?
Agence communication evenementielle Skikda https://foamghost28.wordpress.com/2018/03/19/agence-web-execution-et-innovation-de-la-ville-web/
I really enjoy this template you have got going on in your site. What is the name of the design by the way? I was thinking of using this style for the blog I am going to put together for my class project.
Communication evenementielle Skikda http://apss.org.np/index.php?qa=ask