开发者

update database without refreshing the page using a hyperlink

开发者 https://www.devze.com 2023-03-15 15:49 出处:网络
How can i create something similar to the Facebook LIKE hyperlink which allows me to update开发者_StackOverflow社区 mysql database without refreshing the page?

How can i create something similar to the Facebook LIKE hyperlink which allows me to update开发者_StackOverflow社区 mysql database without refreshing the page? In other words , I need this hyperlink to update the database once i click it and display how many likes is stored in DB without page refresh. Thanks in advace.


In plain simple words, you will need to use AJAX, which will get fired when you click the hyperlink, using JavaScript.

There are these options to use AJAX:-

  1. Use JavaScript own functions to fire AJAX.
  2. Use JavaScript libraries, like jQuery, Prototype, and some more.

By far, jQuery will suit every novice to its best & you can have a look in here for more details on AJAX.

Hope it helps.


in even simpler words than my predecessor;

this is what you have

//html
a href='somewhereOverTheRainbow'>LikeThis...

this is what you should have

//javascript
var likeIt=function(myAnchorElem){
    //send info to ajax via Zepto, jQuery, Mootools, Dojo, ExtJS - you name it - or a standalone ajax lib
    jQuery.get("somewhere.overtherainbow.com/like.php?url="+myAnchorElem.url);

    //prevent the default
    return false;
}

//html
a href='somewhereOverTheRainbow' onclick='return likeIt(this)'>LikeThis...

@javascriptWizards; I know, he should use addEventListener instead to then get a real event on which he can call preventDefault and more.

next to the ajax way, you could use json-p, an img or an iframe or even by using websockets. But for simplicity and ease, stick with the ajax way!

in general; making a feature such as the facebook like or google "+1" seams very trivial. The truth is far from it; it is one of the harder things to do in the web! The Frontend for it is easy like cake. But the Backend... wanting your website to scale and demanding/needing normal database respond times will bring you on to your knees


I'd suggest the AJAX approach but just to mention it, the effect could be achieved without AJAX by placing the button in an iframe, this iframe could then follow the the link without the page having to refresh.


http://infrequently.org/07/OSCON/sample_code.pdf

http://webdeveloper.econsultant.com/ajax-demos-examples-code-samples/

Some code sample

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号