vellai durai vellai durai - 9 months ago 190
Javascript Question

How to pass data-index value of anchor tag to controller in code igniter

I need to pass the

value of an anchor tag to controller in CodeIgniter.

Here is my view:

for (i=0;i<5;i++){
<a href="#" id="testing" data-index="id[$i]"id="testing">testlink</a>

Here is JQuery:

$('#testing').click(function() {
var a = $(this).data("index");

I need to post the value of
var a
to controller of CodeIgniter. How can I pass this value?


You could try something like this:

<?php for ($i=0; $i<5; $i++){?>
    <a href="#" class='testing' data-index="<?= $i;?>" >testlink</a>
<?php }? >

And then tha AJAX:

 $('.testing').click(function() {

            url : "path_to_your_controller",
            data: {
                id: $(this).data("index")
            type: "POST"
            success: function(data, textStatus, jqXHR)
            error: function (jqXHR, textStatus, errorThrown)
              console.log('we are in error');