Link to home
Start Free TrialLog in
Avatar of Michelle Jackson
Michelle Jackson

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial