Jeremygolf
Mitglied
Hallo zusammen,
ich arbeite gerade an einem kleinen Sozialen Netzwerk. Ich habe ein Follower-System.
Nun wenn ich die Profilseite lade schaue ich mit php und mysql nach ob ich diesem User schon folge und zeige somit den richtigen Button dar.
Wenn ich nun auf den Follow button drücke muss ich mit AJAX einen Eintrag in die Datenbank machen und der Button soll sich auf Unfollow ändern.
[/CODE]
$(function() {
$("#follow").click(function() {
var myData = 'follow='+ $("#follow").val();
jQuery.ajax({
type: "POST",
url: "plugins/follow/followunfollow.php",
data: myData,
success: function(data) {
$("#follow").hide();
$(".follow-status").append('<button class="unfollowbtn" id="unfollow" value="<?php echo($userid); ?>">
Unfollow
</button>');
}, error: function () {
alert(" Can't do");
},
});
});
$("#unfollow").click(function() {
var myData = 'unfollow='+ $("#unfollow").val();
jQuery.ajax({
type: "POST",
url: "plugins/follow/followunfollow.php",
data: myData,
success: function(data) {
$("#unfollow").hide();
$(".follow-status").append('<button class="followbtn" id="follow" value="<?php echo($userid); ?>">
Follow
</button>');
}, error: function () {
alert(" Can't do");
},
});
});
});
Kann mir hier jemand weiterhelfen oder eine bessere Lösung vorschlagen.
Vielen Dank Leute
ich arbeite gerade an einem kleinen Sozialen Netzwerk. Ich habe ein Follower-System.
Nun wenn ich die Profilseite lade schaue ich mit php und mysql nach ob ich diesem User schon folge und zeige somit den richtigen Button dar.
Code:
<div class="follow-status">
<?php
if($username != $_SESSION["username"]) {
$currentUser = $_SESSION["userid"];
$sql = mysql_query("SELECT * FROM following WHERE from_user = '$currentUser' AND to_user = '$userid'");
if(mysql_num_rows($sql) > 0) { ?>
<button class="unfollowbtn" id="unfollow" value="<?php echo($userid); ?>">
Unfollow
</button>
<?php
} else { ?>
<button class="followbtn" id="follow" value="<?php echo($userid); ?>">
Follow
</button>
<?php
}
}
?>
</div>
Wenn ich nun auf den Follow button drücke muss ich mit AJAX einen Eintrag in die Datenbank machen und der Button soll sich auf Unfollow ändern.
[/CODE]
$(function() {
$("#follow").click(function() {
var myData = 'follow='+ $("#follow").val();
jQuery.ajax({
type: "POST",
url: "plugins/follow/followunfollow.php",
data: myData,
success: function(data) {
$("#follow").hide();
$(".follow-status").append('<button class="unfollowbtn" id="unfollow" value="<?php echo($userid); ?>">
Unfollow
</button>');
}, error: function () {
alert(" Can't do");
},
});
});
$("#unfollow").click(function() {
var myData = 'unfollow='+ $("#unfollow").val();
jQuery.ajax({
type: "POST",
url: "plugins/follow/followunfollow.php",
data: myData,
success: function(data) {
$("#unfollow").hide();
$(".follow-status").append('<button class="followbtn" id="follow" value="<?php echo($userid); ?>">
Follow
</button>');
}, error: function () {
alert(" Can't do");
},
});
});
});
Code:
Hier ist noch das PHP script
[CODE]
<?php
include("../../php/auth.php");
include("../../php/connect.php");
if(isset($_POST["follow"])){
$userid = $_POST["follow"];
$currentUser = $_SESSION["userid"];
$query = mysql_query("INSERT INTO following (from_user, to_user) VALUES ('$currentUser', '$userid')");
if($query) {
echo("true");
} else {
die(mysql_error());
}
} else if(isset($_POST["unfollow"])){
$userid = $_POST["unfollow"];
$currentUser = $_SESSION["userid"];
$query = mysql_query("DELETE FROM following WHERE from_user = '$currentUser' AND to_user = '$userid'");
if($query) {
echo("true");
} else {
die(mysql_error());
}
}
?>
Kann mir hier jemand weiterhelfen oder eine bessere Lösung vorschlagen.
Vielen Dank Leute