简体   繁体   English

如何在我的网站上添加关注-取消关注按钮?

[英]How to add a Follow - UnFollow Button on my Website?

I want to make a button on my website so that a user can click follow OR Unfollow on a certain game. 我想在我的网站上创建一个按钮,以便用户可以在某个游戏上单击“关注”或“取消关注”。

Here is my follow Table: Follow Button Table 这是我的关注表: 关注按钮表

When a user clicks on the button associated with the game ID, it should insert 1 into the game_follow database for that id. 当用户单击与游戏ID关联的按钮时,应在该ID的game_follow数据库中插入1。

(I already have a game and users table, and I know how to get each of those ID's) (我已经有一个游戏和用户表,而且我知道如何获取每个ID)

I know this is a broad question, but maybe someone can point me in the right direction, or show me another tutorial. 我知道这是一个广泛的问题,但是也许有人可以指出正确的方向,或者向我展示另一个教程。

EDIT: This so far isn't working: 编辑:到目前为止,这是行不通的:

Heres my button: 这是我的按钮:

 <button class="follow-game" data-game_id="<?php echo $game_id['id']; ?>">Follow!</button> 

Heres my ajax call: 这是我的ajax电话:

 $(function(){ $('.follow-game').click(function(){ follow_game(this); }); }); function follow_game(obj){ var game_id = $(obj).attr('data-game_id'); jQuery.ajax({ url: 'follow.php', type: 'POST', data: { game_id : game_id }, success: function(data) { alert("You Followed!"); }, error: function () { alert("Something went wrong with Follow Button."); } }); } 

And here is my follow.php 这是我的follow.php

 <?php require_once 'core/init.php'; // Set user_id to the currently logged in user. $user_id = $user_data['id']; // Set $game_id to the current ID of the game (coming from ajax call) $game_id = $_POST['game_id']; // Grab the game (ID) from the games table, then Query it. //$SQL_SELECT_GAME = "select * from games where id = '$game_id'"; //$db->query($SQL_SELECT_GAME); // Do an update on game_follow table, and set follow = 1 (means that this game is being followed) where the user_id = $user_id and game_id = $game_id, then Query it. $SQL_UPDATE = "update game_follows set follow = 1 where user_id = '$user_id' and game_id = '$game_id'"; $db->query($SQL_UPDATE); 

My table is the image link above 我的桌子是上面的图片链接

Definitely a broad question, so, broad answers: 绝对是一个广泛的问题,因此,广泛的答案:

My suggestion would be to attach a javascript listener to the button, which will call an ajax function running a php script, executing the mysql query to update you database accordingly. 我的建议是将一个Javascript侦听器附加到该按钮,该按钮将调用运行php脚本的ajax函数,执行mysql查询以相应地更新您的数据库。

For the html: 对于html:

<button class="follow-game" data-game_id="1">Follow!</button

For the javascript: (jquery is my preference, so that's my example) 对于javascript:(jquery是我的偏爱,所以这是我的示例)

$(function(){
   $('.follow-game').click(function(){
      follow_game(this);
   });
});
function follow_game(obj){
game_id = $(obj).attr('data-game_id');

$.ajax({
  url: '/follow.php',
  type: 'POST',
  dataType: 'json',
  data: {
    game_id : game_id
  },
  success: function(rs){
    alert("yay");
  }
});

}

For the PHP/MYSQL: 对于PHP / MYSQL:

$user_id = $_SESSION['user_id'];
$game_id = $_POST['game_id'];
$sql = "UPDATE `game_follows` SET follow = 1 WHERE user_id='{$user_id}' AND $game_id='{$game_id}'";

Then run the sql with whatever mysql connection/method you're using. 然后使用您正在使用的任何mysql连接/方法运行sql。 Of course, this doesn't take security into consideration at all, etc, but worry about that once you have the basic functionality down. 当然,这根本不考虑安全性,等等,但是一旦基本功能失效,就不必担心。

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

相关问题 如何在我的网站中添加 Instagram 关注按钮? - How to add Instagram Follow button in my website? JavaScript 关注/取消关注按钮 - JavaScript Follow / Unfollow Button 角度指令跟随/取消跟随按钮 - Angular Directive Follow/Unfollow button 我需要在我的网站上添加 Instagram 关注按钮,用户可以在不重定向 Instagram 网站的情况下关注 - I need to add Instagram follow button on my website where user can follow without redirecting the instagram website 在 React.js 中实现关注和取消关注按钮 - Implement follow and unfollow button in React.js 使用php和html跟随/取消关注按钮 - Follow / unfollow button using php and html 使用ajax在rails中呈现跟随/取消跟随按钮 - render follow/unfollow button in rails with ajax jQuery / Ajax-跟随/取消跟随按钮,重复功能 - jQuery/Ajax - Follow/Unfollow button, repeating functions 怎么可能有人使用Twitter Bootstrap在Twitter的网站上复制Follow / Unfollow悬停操作? - How could someone replicate the Follow/Unfollow hover action on Twitter's website using Twitter Bootstrap? 单击关注,取消关注按钮时,如何仅在 Vuetify.js 中做出反应和停用该按钮 - How to react and deactivate only that button in Vuetify.js when the follow, unfollow button is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM