1

I wanted to change an input value based on the selection from a combobox in codeigniter. I tried to code it but theres nothing to be displayed. Here is my code in my controller.....

function fill_info()
{
    // retrieve the group and add to the data array
    $group_id = $this->input->post('group_id');
    $data = "0.00";
    if($group_id)
    {
        $this->load-model('Base_amount_setting_model');
        $baseamount = $this->Base_amount_setting_model->getbaseamount($group_id);
        $data .= $baseamount;
        echo $data;
    }
    else 
    {
        echo $data;
    }
}

And in my Base_amount_setting_model there is this method....

function getbaseamount($group_id)
{
    $this->db->where('group_id',$group_id);
    $baseamount = $this->db->get('base_amount_setting')->row()->amount;
    if($baseamount -> num_rows() == 1)
    {
        return $baseamount->result();
    }
}

And there in my view the ajax looks like this.....

<script>
    $(document).ready(function() 
    {
        $("#group_id").change(function() 
        {
            var group_id = $("#group_id").val();
            $.ajax({
                type : "POST",
                url  : "<?php echo base_url('payment/fill_info'); ?>",
                data : "group_id=" + group_id,
                success: function(data) 
                {
                    $("#base_amount").html(data);
                }
            });
        });
    });
</script>

and finally my form is like this...

<div class="control-group">
    <label class="control-label" for="select01">Group Id </label>
    <div class="controls">
        <select class="chzn-select" name="group_id" id="group_id" placeholder="Group Id" value="<?php echo $group_id; ?>">
            <option></option>
            <?php
            if (count($groups)) {
                foreach ($groups as $list) {
                    echo "<option value='". $list['group_id'] . "'>" . $list['group_name'] . "</option>";
                }
            }
            ?>
        </select>
        <label for="int" class="err"><?php echo form_error('group_id') ?></label>
    </div>
    <input class="input-xlarge disabled" id="base_amount" name="base_amount" type="text" placeholder="Base Amount" disabled="">
    <input class="input-xlarge disabled" id="total_members" type="text" placeholder="Total Members" disabled="">
</div>

Thank You!

1
  • you should change $("#base_amount").val(data); Commented Sep 15, 2016 at 10:03

3 Answers 3

2

change this $("#base_amount").html(data); to $("#base_amount").val(data);

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

1 Comment

That was helpful.
1

Actually if you get your value after ajax hit in data object then only change this :

$("#base_amount").html(data);

to

$("#base_amount").val(data);

Actually .html replce the html not change the value.

Comments

0

i'm really curious - but i think your model doesnt return anything try the following

function getbaseamount($group_id)
{
    $query = $this->db
        ->where('group_id',$group_id)
        ->get('base_amount_setting');

    if ($query->num_rows() == 1)
    {
        $obj = $query->row();
        return $obj->amount;
    }
}

and as others suggested

change your script code to

$("#base_amount").val(data);

and your controller function should look like

function fill_info()
{
    // retrieve the group and add to the data array
    $group_id = $this->input->post('group_id');
    $data = "0.00";
    if($group_id)
    {
        $this->load-model('Base_amount_setting_model');
        $baseamount = $this->Base_amount_setting_model->getbaseamount($group_id);
        echo $baseamount;
    }
    else 
    {
        echo $data;
    }
}

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.