vellai durai vellai durai - 6 months ago 161
Javascript Question

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

I need to pass the

data-index
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:

<script>
$('#testing').click(function() {
var a = $(this).data("index");
alert(a);
});
</script>


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

Answer

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() {

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