Aditya Pandey Aditya Pandey - 5 months ago 15
PHP Question

How to call ajax for a given list anchor tags with same class id but different url to call

I'm new to AJAX, and start to make an ajax able script, in which, I have a number of anchor tags with same class id. But with different url to call for each anchor tag. My function runs but, it call all the anchor tag, even i use to click on one anchor tag. But the task is done for only that clicked anchor tag.

My 2nd problem is

when the task completed for one anchor tag and I start to run other ones, it does not complete the task until I refresh the web page again, and click on it. But it shows that success on every anchor tag. Even the work is not done. Hope you understand my problem, Here is my ajax code.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
$('.submitdelete').click(function() {
$.ajax({
type:'POST',
url: 'http://localhost/w/wp-admin/admin.php?page=rssap-feeds&action=removePosts&post=28',
success: function(response) {
alert("You have made it!");
}
});
});
</script>
<a href="#" title="Remove Posts" class="submitdelete">Remove Posts</a>
//this is for below one
<script type="text/javascript">
$('.submitdelete').click(function() {
$.ajax({
type:'POST',
url: 'http://localhost/w/wp-admin/admin.php?page=rssap-feeds&action=removePosts&post=29',
success: function(response) {
alert("You have made it!");
}
});
});
</script>
<a href="#" title="Remove Posts" class="submitdelete">Remove Posts</a>


And if some one does not understand this problem, please comment below for more information.

Question is How do I do that correctly?

Answer

You can proceed, By making a function in javascript and use it in your anchor tag, I am using giorgio method, you have to use the attribute data-post-id. So here is my code,

                <script type="text/javascript">         
function callbb(postID){
$.ajax({
type:'POST', 
url: 'http://localhost/w/wp-admin/admin.php?page=rssap-feeds&action=removePosts&post=' + postID + '', 
success: function(response) {
 alert('you made it ' + postID + '');
}
});
}
</script>

And your anchor tag will look like something,

 <a href="#" data-post-id="29" onclick="var postID = $(this).data('postId'); callbb(postID)" title="Remove Posts" class="submitdelete">Remove Posts</a>

Hope it helps you. or someone else

Comments