简体   繁体   English

更新数据库而不使用超链接刷新页面

[英]update database without refreshing the page using a hyperlink

How can i create something similar to the Facebook LIKE hyperlink which allows me to update mysql database without refreshing the page?如何创建类似于 Facebook LIKE 超链接的东西,它允许我在不刷新页面的情况下更新 mysql 数据库? 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.简单来说,您将需要使用 AJAX,当您单击超链接时,它将被触发,使用 JavaScript。

There are these options to use AJAX:-有这些选项可以使用 AJAX:-

  1. Use JavaScript own functions to fire AJAX.使用 JavaScript 自己的函数来触发 AJAX。
  2. Use JavaScript libraries, like jQuery , Prototype , and some more.使用 JavaScript 库,例如jQueryPrototype等等。

By far, jQuery will suit every novice to its best & you can have a look in here for more details on AJAX.到目前为止,jQuery 将最适合每个新手,您可以在此处查看有关 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; @javascriptWizards; I know, he should use addEventListener instead to then get a real event on which he can call preventDefault and more.我知道,他应该使用addEventListener来获得一个真实的事件,他可以在该事件上调用 preventDefault 等等。

next to the ajax way, you could use json-p, an img or an iframe or even by using websockets.在 ajax 方式旁边,您可以使用 json-p、img 或 iframe 甚至使用 websockets。 But for simplicity and ease, stick with the ajax way!但为了简单起见,请坚持使用 ajax 方式!

in general;一般来说; making a feature such as the facebook like or google "+1" seams very trivial.使诸如 facebook 之类的功能或谷歌“+1”接缝非常微不足道。 The truth is far from it;事实远非如此; it is one of the harder things to do in the web.这是 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. 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM