我需要將錨標記的data-index
值傳遞給CodeIgniter中的控制器。如何將錨標記的數據索引值傳遞給代碼點火器中的控制器
這是我的觀點:
<?php for ($i=0; $i<5; $i++){?>
<a href="#" class='testing' data-index="<?= $i;?>" >testlink</a>
//need to display the json data i am recieving from jquery here
<?php }? >
}
這裏的JQuery:
$('.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');
}
});
這裏是我的控制器
$data= array('value' =>22,
'value2' => 32,
'value3' => 'foo',
'value4' => 'bar',
'value5' => '122',
);
echo json_encode($data);
如何在PHP中
顯示來自Ajax請求的JSON數據
嘗試變種a = $(本).attr( '數據索引'); – Poria
ID必須是唯一的 – C2486
http://api.jquery.com/jquery.ajax/ – sinisake