2016-08-01 66 views
0

我需要使用帶有函數值的ajax。 我有,例如標籤與「ID = 36」,我在HTML中度日的OnClick:ajax與codeigniter中的函數值

id="<?=$row['customer_id'];?>" onclick="test(this.id)"> 

現在我需要努力低谷吧。 我的java腳本代碼:

<script type="text/javascript"> 
    function test(clicked_id) {  
     var value = { 'id' : 36; }; 
     $.ajax({ 
      type: 'POST', 
      url: '<?php echo site_url('test/ret(id)'); ?>', 
      data: value, 
      success: function(resp) { 
       $('#data').html(resp); 
      } 
     }); 
    } 
</script> 

我知道一些錯誤!但什麼?! 身份證我得到我正確的,但我怎麼能發送和使用它在Java腳本? 的VAR值沒有得到「身份證」,我認爲

url: '<?php echo site_url('test/ret(id)'); ?>' 

實在是太不正確!我如何發送ID槽功能?

+0

[編輯](http://stackoverflow.com/p osts/38703824 /編輯)問題並顯示您的控制器代碼。 – DFriend

+0

你不需要URL中的'id'。它已經通過POST變量傳遞。只需在'ret()'方法的'Test.php'控制器中用'$ this-> input-> post('id')'來獲取它。 – Tpojka

回答

0
<script type="text/javascript"> 
function test(clicked_id) {  

    $.ajax({ 
     type: 'POST', 
     url: '<?php echo site_url('test'); ?>', 
     data: { id: clicked_id }, 
     success: function(resp) { 
      $('#data').html(resp); 
     } 
    }); 
} 

,並使用該網頁上獲得

$id = $_POST['id']; 
0

感謝您的回覆......特別是@Tpojka :) 問題在「var id = 36」後面有分號,而Tpojka說,id將通過POST並且不需要寫入「test/ret( ID)」。 工作代碼爲:

<script type="text/javascript"> 
    function test(clicked_id) {  
     var value = { 'id' : clicked_id }; 
     $.ajax({ 
      type: 'POST', 
      url: '<?php echo site_url('test/ret'); ?>', 
      data: value, 
      success: function(resp){ 
       $('#error').html(resp); 
      } 
     });  
    } 
</script> 

,並在RET方法測試控制器,我只是抓住ID與

$this->input->post('id')