3

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?

4
  • 2
    You have to call a controller which will call your helper and return the results Commented Mar 7, 2015 at 10:49
  • i tried but it does't show the return value . Commented Mar 7, 2015 at 13:56
  • Any error ? Test your controller first, make sure it returns something. If it does, then check your ajax call. Commented Mar 7, 2015 at 13:58
  • it return but as undefine Commented Mar 7, 2015 at 14:22

2 Answers 2

2

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));
}
Sign up to request clarification or add additional context in comments.

2 Comments

when i return data success:function(){ return data;} it gives undefined
You shoud use data.price
-1

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....

4 Comments

thanks for replay but you are not getting me . i want to called helper function .
as per my understanding u want to use the helper function in jquery..so <?php if(helper_function($variable)) { ?> will call your helper function...
my variable is var currency_id,and and var total_amout,and it cannot be passed like <?php if(helper_function(total_amount,currency_id)) { ?> because these are jquery variables .
u can use php and/or jquery for total_amount and currency_id....before call the helper method......you are trying to break the mvc structure...so you have to do something like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.