2016-08-01 63 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')