duangao7133
2015-03-07 10:42
浏览 84
已采纳

如何使用JQuery AJAX调用codeigniter辅助函数?

I have a function defined in my helper function in codeigniter that returns the formatted price when val and currency id is passed to it.

if(!function_exists('format_price')){
function format_price($val,$curency_id=NULL){
    $CI =& get_instance();
    $CI->load->model('currency_model');
    if ($curency_id) {
        $Result=$CI->currency_model->getcurrency($curency_id);
        $dec_place=round($Result['decimal_place']);
        $value=number_format((float)$val,$dec_place,'.',',');
        return $Result['symbol_left'].$value ." ".$Result['symbol_right'];
    }
    else{
        $Result=$CI->currency_model->getDefaultCurrency();
        $dec_place=round($Result['decimal_place']);
        $value=number_format((float)$val,$dec_place,'.',',');
        return $Result['symbol_left'].$value ." ".$Result['symbol_right'];
    }
   }
}

What I need is to call this function through ajax in javascript code. is this possible without a controller, or do I have to make a controller?

图片转代码服务由CSDN问答提供 功能建议

我在codeigniter中的助手函数中定义了一个函数,当val和currency id传递给我时,它返回格式化的价格 它。

  if(!function_exists('format_price')){
function format_price($ val,$ curency_id = NULL){
 $ CI =&  get_instance(); 
 $ CI-> load-> model('currency_model'); 
 if($ curency_id){
 $ Result = $ CI-> currency_model-> getcurrency($ curency_id);  
 $ dec_place = round($ Result ['decimal_place']); 
 $ value = number_format((float)$ val,$ dec_place,'。',','); 
返回$ Result ['symbol_left'  ]。$ value。“”。$ Result ['symbol_right']; 
} 
 else {
 $ Result = $ CI-> currency_model-> getDefaultCurrency(); 
 $ dec_place = round($ Result  ['decimal_place']); 
 $ value = number_format((float)$ val,$ dec_place,'。',','); 
返回$ Result ['symbol_left']。$ value。“”。$ 结果['symbol_right']; 
} 
} 
} 
   
 
 

我需要的是通过javascript代码中的ajax调用此函数。 这是 可能没有控制器,或者我是否需要制作控制器?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doucheng9304 2015-03-08 07:35
    已采纳

    You need to make a request via controller, then call that function through that controller, something like:

    $("#id").change(function()
    {       
     $.ajax({
         type: "POST",
         url: base_url + "controller_name/your_function", 
         data: {val: $("#your_val").val(),currency_id: $("#your_cur").val()},
         dataType: "JSON",  
         cache:false,
         success: 
              function(data){
                $("#your_elem").val(data.price);
          }
    });
    

    Then on your controller:

    public function yourfunction()
    {
       $data = $this->input->post();
       $price = format_price($data['val'],$data['currency_id']);
       echo json_encode(array('price' => $price));
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doupuxuan5784 2015-03-07 11:05

    instead of using ajax try like this...

    <script type="text/javascript">
      $(document).ready(function(){
        (function(){
          <?php if(helper_function($variable)) { ?>
              now your jquery script..........
          <?php } ?>
        });
      });
    </script>
    

    customize above code as u want....

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题