0

I'm wondering why it doesn't work for me. I followed instructions from here: http://stackoverflow.com/questions/5418179/problem-with-jquery-getjson-retrieve-data-from-controller-code-igniter but maybe I'm doing something wrong. I'm trying to make script that will populate the rest of the fields when user selects a value from select menu.

My jQuery script looks like this:

$(document).ready(function() {
    $('#selprod-1').change(function() {
        var id = $('#selprod-1 option:selected').val();
        $.post(
            '<?php echo $head['site_link'];?>faktura/pobierzProdukt/'+id,
            function(data){
                alert(data);
                $('#pkwiu').val(data.product_pkwiu);
                $('#netto').val(data.product_netto);
                $('#vat').val(data.product_vat);
                $('#brutto').val(data.product_brutto);
                $('#jedn').val(data.product_jedn);
            },
            'json'
        );
    });
});

My HTML form looks like this:

<tr>
<td>1</td>
<td><?php echo form_dropdown('produkt[]',$lista,'','id="selprod-1"');?></td>
<td><?php echo form_input(array('name'=>'pkwiu','class'=>'short','readonly'=>'readonly'));?></td>
<td><?php echo form_input(array('name'=>'netto','class'=>'short','id'=>'netto','readonly'=>'readonly'));?></td>
<td><?php echo form_input(array('name'=>'vat','class'=>'mini','id'=>'vat','readonly'=>'readonly'));?></td>
<td><?php echo form_input(array('name'=>'brutto','class'=>'short','id'=>'brutto','readonly'=>'readonly'));?></td>
<td><?php echo form_input(array('name'=>'jedn','class'=>'mini','id'=>'jedn','readonly'=>'readonly'));?></td>
<td><?php echo form_input(array('name'=>'ilosc','class'=>'short','id'=>'ilosc'));?></td>
<td><?php echo form_input(array('name'=>'knetto','class'=>'short','id'=>'knetto','readonly'=>'readonly'));?></td>
<td><?php echo form_input(array('name'=>'kvat','class'=>'short','id'=>'kvat','readonly'=>'readonly'));?></td>
<td><?php echo form_input(array('name'=>'kbrutto','class'=>'short','id'=>'kbrutto','readonly'=>'readonly'));?></td> </tr>

My controller function (which works) looks like this:

public function pobierzProdukt($id)
    {
        $json = json_encode($this->Faktura_model->getProduct($id));
        header('Cache-Control: no-cache, must-revalidate');
        header('Expires: Mon, 26 Jul 2027 05:00:00 GMT');
        header('Content-type: application/json');
        echo $json;
    }

My model function looks like this:

public function getProduct($id)
    {
        $q = 'SELECT product_vat, product_netto, product_brutto, product_jedn
            FROM products
            WHERE product_id="'.$id.'"';
        $w = $this->db->query($q);
        return json_encode($w->row_array());
    }
3
  • ur encoding it twice, not sure if json_encode deals with that or not but still $json should not be json_encode Commented May 16, 2011 at 12:43
  • Thanks a lot. I didn't notice it before. Commented May 16, 2011 at 13:42
  • You can mark the answer below lol :) Commented May 17, 2011 at 8:51

1 Answer 1

0

ur encoding it twice, not sure if json_encode deals with that or not but still $json should not be json_encode

Sign up to request clarification or add additional context in comments.

Comments

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.