Jump to content
Sign in to follow this  
helloworld001

I need a fresh pair of eyes to look at this code and tell me what's wrong with it.

Recommended Posts

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';

}	

Share this post


Link to post
Share on other sites
"unfollow="+unfollow+"follower="+follower+"&action=unfollow"
Missing a &

Share this post


Link to post
Share on other sites
"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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.