简体   繁体   English

像按钮一样的Facebook实现

[英]implemantation of facebook like button

i am developing a site with html and php ... 我正在用html和php开发网站...

what i have done so far for like button on my page is as follow(concept is same only name is different on facebook it is like and on my site it is Points Up ) 到目前为止,我对页面上的“喜欢”按钮所做的操作如下(概念是相同的,只是在Facebook上的名称不同,而在我的网站上是Points Up)

<form method="post">
<input type="hidden" value="<?php echo $posts[postid]; ?>" name="postid">
<input type="submit" name="pointsup" value="Points Up" />
</form>

the above will create the button with name Points Up. 以上将创建名称为Points Up的按钮。

if(isset($_POST['pointsup'])) { in this if block i have written all queries to update database and user interface and all }

what i want is instead of that button there should be some link witch will run my sql code. 我想要的是代替该按钮,应该有一些链接可以运行我的sql代码。

i also tried JavaScript but it doesn't help anything 我也尝试过JavaScript,但无济于事

Thanks in advance for your answers! 预先感谢您的回答!

When you are working with a single anchor tag (hyperlink), you should send your parameters by GET method. 当使用单个锚标记(超链接)时,应通过GET方法发送参数。 in case, /your/address/rate.php?id= and check $_GET['id'] in PHP (validate/sanitize/...) 万一,/ /your/address/rate.php?id= $_GET['id'] /your/address/rate.php?id=并在PHP中检查$_GET['id'] (验证/清除/ ...)

also, you can send your requests as POST by using AJAX. 另外,您可以使用AJAX将请求发送为POST。 this is a sample for jQuery: 这是jQuery的示例:

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
}).done(function( msg ) {
  alert( "Data Saved: " + msg );
});

take a look here 在这里看看

but if you want to use pure JavaScript, you should work with XMLHttpRequest object. 但如果要使用纯JavaScript,则应使用XMLHttpRequest对象。 take a look here . 看一下这里

You should do something like: 您应该执行以下操作:

  1. Find where you should point your link to and what parameters are needed, i will call it $url. 找到您应该指向链接的位置以及需要哪些参数,我将其称为$ url。 It can involve such things like creating additional files, refactoring your code, etc... 它可能涉及诸如创建其他文件,重构代码等之类的事情。
  2. Write js method which will send request to $url. 编写js方法将请求发送到$ url。 I will call this method sendPointsUp; 我将这种方法称为sendPointsUp;
  3. Create link which will react to onClick event and call sendPointsUp with desired parameters from there. 创建将对onClick事件做出反应的链接,并从此处使用所需参数调用sendPointsUp。

What you can do is just add a link with an id so you can reach it easily with jquery. 您可以做的就是添加一个带有ID的链接,以便可以通过jquery轻松访问它。
Create a php-page where you do the query and other stuff (security check, cookie, whatever you want to do), and finally attach an ajax event to the link to call your php-page. 创建一个php页,您可以在其中执行查询和其他操作(安全检查,cookie,无论您想做什么),最后将ajax事件附加到链接上以调用php页。

Print the link in your page: 在页面中打印链接:

<a href="" id="voteup">Vote up</a>

Create php 创建PHP

<?php
  // do the query and other stuff, return the result in json format
  // if you want to do something with the result (for example display the votes)

Javascript Java脚本

$('#voteup').click(function(e){
  $.ajax({
  url: "your/url/to/phpfile",
}).done(function(data) { 
  // rewrite link to undo vote or whatever you want
  // do something with the returned data
});
});

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

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