helloworld001 Posted March 14, 2015 Share Posted March 14, 2015 I have done this a while back but lost the code. It worked back then. I am trying to recreate it but my memory isn't perfect. It's basically a follow/unfollow button like twitter. This is the main reference. http://jsfiddle.net/jakie8/ZECEN/ The php/mysql action queries do work if I call it through normal a href link; but for some reason, it's not being called through ajax. The jquery/ajax set variables do show up correctly when I test an alert with them inside. So that works too. I could really use fresh pair of eyes to see what I might have done wrong. Here is the index.php page. <html> <head> <script src="js/jquery-1.11.0.min.js"></script> <script> $(function() { $(document).on('click','.followButton',function(e) { e.preventDefault(); $button = $(this); if($button.hasClass('following')){ //$.ajax(); Do Unfollow var unfollow = $(this).attr('rel'); var follower = $("#follower-id").val(); $.ajax({ type: "POST", url: "actions.php", data: "unfollow="+unfollow+"follower="+follower+"&action=unfollow", success:function(data){ $button.removeClass('following'); $button.removeClass('unfollow'); $button.text('+ Follow'); } }); } else { // $.ajax(); Do Follow var following = $(this).attr('rel'); var follower = $("#follower-id").val(); $.ajax({ type: "POST", url: "actions.php", data: "following="+following+"follower="+follower+"&action=follow", success:function(data){ $button.addClass('following'); $button.text('Following'); } }); // return false; } }); $('button.followButton').hover(function(){ $button = $(this); if($button.hasClass('following')){ $button.addClass('unfollow'); $button.text('Unfollow'); } }, function(){ if($button.hasClass('following')){ $button.removeClass('unfollow'); $button.text('Following'); } }); }); </script> </head> <body> <div class="container"> <input type="hidden" id="follower-id" value="<?php echo $follower_userid; ?>"> <button class="btn followButton following" rel="<?php echo $following_userid; ?>">+ Follow</button> </div> </body> </html> And here is actions.php page. require_once 'core/init.php'; if($action == "follow") { $following = $_POST['following']; $follower = $_POST['follower']; $date = date('Y-m-d H:i:s'); $follow = $db->prepare("INSERT INTO followers(following_id, follower_id, date, active) VALUES(:following_id, :follower_id, :date, :active)"); $follow->bindParam(':following_id', $following); $follow->bindParam(':follower_id', $follower); $follow->bindParam(':date', $date); $follow->bindValue(':active', 1); $follow->execute(); } else if($action == "unfollow") { $unfollow = $_POST['unfollow']; $follower = $_POST['follower']; $date = date('Y-m-d H:i:s'); $unfollow = $db->prepare("UPDATE followers SET date = :date, active = :active WHERE following_id = :following_id AND follower_id = :follower_id"); $unfollow->bindParam(':following_id', $unfollow); $unfollow->bindParam(':follower_id', $follower); $unfollow->bindParam(':date', $date); $unfollow->bindValue(':active', 0); $unfollow->execute(); } else { // echo 'no action'; } Quote Link to comment https://forums.phpfreaks.com/topic/295218-i-need-a-fresh-pair-of-eyes-to-look-at-this-code-and-tell-me-whats-wrong-with-it/ Share on other sites More sharing options...
Solution requinix Posted March 14, 2015 Solution Share Posted March 14, 2015 "unfollow="+unfollow+"follower="+follower+"&action=unfollow"Missing a & Quote Link to comment https://forums.phpfreaks.com/topic/295218-i-need-a-fresh-pair-of-eyes-to-look-at-this-code-and-tell-me-whats-wrong-with-it/#findComment-1508043 Share on other sites More sharing options...
helloworld001 Posted March 14, 2015 Author Share Posted March 14, 2015 "unfollow="+unfollow+"follower="+follower+"&action=unfollow"Missing a & Ahh yes that's it. I knew it was something small. Good thing I asked; otherwise I would have spent the entire night figuring this out. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/295218-i-need-a-fresh-pair-of-eyes-to-look-at-this-code-and-tell-me-whats-wrong-with-it/#findComment-1508053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.