[ajax] How to ajax make it so that clicking on randomname number of links on the page?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
19 views
Links on the page are displayed I have this php code

<?php foreach ($ids as $itemIds): ?>Click the link 1
Click the link 2
Click on the link 3<?php endforeach; ?>


In the array $ids the identifiers which are previously taken from the database. Such identifiers at different periods of time may be a different number.
These identifiers form the parameters for each link. It all works well. But we need this whole thing translated into ajax.
So, if for example, I clicked on the link id="url-1" the response from the server is displayed inside the tag
And so on each link on the page.

=================================================================================
Now go to ajax. I have this code for ajax based on jquery.

function funcBefore(){$("#info-1").text("Ожидание данных");}function funcSuccess(data){$("#info-1").text(data);}$(document).ready(function(){$("#url-1").on("click", function(){$.ajax({url: "myscript.php",type: "GET",dataType: "html",beforeSend: funcBefore,success: funcSuccess});});});


With the help of it I can handle, I need the elements on the page. But the problem is, how do I handle dynamic links on the page, which can be a different number? And with different parameters for GET request. That is, as I pointed out in the example above.

All I would be very grateful for the help!
by | 19 views

4 Answers

0 like 0 dislike
Add all the links that need to be handled, some class instead of id selector use the selector class in jq. Parameters for a get request and receive from the link when using the same jQuery, create the query string dynamically, after clicking.
by
0 like 0 dislike
url: $(this).attr("href)
by
0 like 0 dislike
all could be simpler:
$('[data-toggle="myloader"]').on('click',function(e){ e.preventDefault(); //get canceled var url = $(this).attr('href'), // took the link container_selector = $(this).attr('data-container'); // get the selector $(container_selector).load(url); //send ajax, html got stuck in the container });

That's all. The main links should be data-toggle="myloader" and each has its own href and your data container, for example:
jMaki gently
by
0 like 0 dislike
Thank you very much! I will try
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,187 answers
0 comments
40,796 users