2016-05-14 37 views
0

我需要將錨標記的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數據
+0

嘗試變種a = $(本).attr( '數據索引'); – Poria

+0

ID必須是唯一的 – C2486

+0

http://api.jquery.com/jquery.ajax/ – sinisake

回答

0

你可以嘗試是這樣的:

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

然後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'); 
      } 
     }); 
+0

請ID唯一#testing –

+0

是的,我忘了打字 10X :) –

+0

時如何得到這個怎麼$在控制器 –

0

而不是調用了ID功能,您必須使用類的

視圖文件中的代碼

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

您的腳本//將dataType傳遞爲json

在您的控制器
$('.testing').click(function() { 
     var a = $(this).data("index"); 
     $.ajax({ 
      type: "POST", 
      dataType: "json", 
      url: 'url to controller', 
      data: {id:a}, 
      success: function(result) { 
       // you can get here the result the result of ajax request. 
       // get each value as key.value(of controller). 
       alert(result.value); 
      }, 
      error: function(a,s,d) { 
       console.log('error'); 
      } 
     }); 
    }); 

得到這個ID作爲

// in controller function get id as like 
$this->input->post('id'); 
+0

如何從jquery獲取值到php,我正在獲取json數據如何將該數據傳遞給php –

+0

,您可以在此查看json數據文件或控制器?你能展示樣品嗎? –

+0

在控制器我越來越json數據..我需要將json數據傳遞給jquery並傳遞到php –

相關問題