Trouble getting ID from class selector to Update Database

I have a mark as favorite resource section in my application that needs to add the resource ID, and user ID to the database so that it will show under the users favorite resources list. The problem that I am having is that no matter which resource the user selects it always adds the first resource listed to the database. I am not getting the proper ID for the resource and need help with this. Here is the code for the favorite resource selection portion.
<aside class="course-details__resources">
						<h3>Resources</h3>
						<p class="instruction">Mark resources as favorites with the heart icon: <img src="images/learncom/resources/favorite-resource--off.png" alt="Favorite resources icon" />.&ensp;Favorite resources appear on the home page for quick access.</p>
						<ul class="resource-list">
						<?php $result_resources= mysqli_query($database_link, ("SELECT * FROM course_resources WHERE CourseID = '$course_id' ORDER BY Filename"));
							while ($rowresources = mysqli_fetch_assoc($result_resources)) {?>	
								
								<li class="resource--pdf"><a href="resources/<?php echo $rowresources['LinkURL']; ?>" target="_blank"><?php echo $rowresources["Filename"] ?></a><input class="favorite-resource-checkbox" id="favorite-resource-checkbox-<?php echo $rowresources["ID"] ?>" type="checkbox" value="<?php echo $rowresources["ID"] ?>" name ="<?php echo $rowresources["Filename"] ?>">
            <label for="favorite-resource-checkbox-<?php echo $rowresources["ID"] ?>"><span>Favorite this resource</span></label></li>
                            <?php } ?>	
						</ul>
					</aside>

Open in new window


Here is where I am having a problem obtaining the proper ID for the checkbox that is clicked:
 $(document).on('click', '.favorite-resource-checkbox', function(){  
            var resourceValues = $( ".favorite-resource-checkbox" ).val();
		    var user = "<?php echo $user_id; ?>";
	 		var id = $(this).attr('id');
           
           $.ajax({  
                url:"file.php",  
                method:"POST",  
                data:{value: resourceValues , user: user},  
                dataType:"text",  
                success:function(data)  
                {  
                     alert(data);  
                    
                }  
           })  
      });

Open in new window


When I set the a=value to resourceValues I get the same resource each time, when I use value:id I get an error stating the the filename cannot be blank. I included both to illustrate everything that I have tried so far. Thanks
Michelle JacksonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
You have an issue with the way your selecting the checkbox:

var resourceValues = $( ".favorite-resource-checkbox" ).val();

That will just select the first one, no matter which one you clicked. You need to use this

var resourceValues = $( this ).val();

Bear in mind that your code will send the ID whenever it's clicked on, regardless of whether it's checked or unchecked! You probably also need to send a bool indicating whether it's checked or unchecked.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.