Now I want to share a small incident occurs recently to me, one of my friend working in company suddenly, he make a call to me regarding a task related to codeigniter, I responded the call and share my thoughts on his task and successfully deployed, I got an idea to share same task which is populating text boxes on selecting dropdown box using codeigniter. It would be helpful for others, looking for same functionality.

If you are looking for SEO Keyword Research tool ,Semrush keyword research tool gives good results in Search engine optimization, which offers 15 day free trial let’s try to, Sign up for semrush account and research your keywords.

I will explain in detail what I had done to execute it successfully, let’s walk through the topic.

In this article, I used post method to pass the selected dropdown id to fetch the values from database table. Let assume a dummy values in the table below.

tbl_subscriptions

<div class="row">
<div class="form-group">
           <label class="col-sm-3 control-label">Featured Subscriptions:</label>
           <div class="col-sm-5">
           <select name="subscription" id="subscription" onChange="get_values()" class="form-control">
           <option value="0" selected="" disabled="">Select State</option>
           <?php foreach($add_fet_Subscription as $row) { ?>
           <option value="<?php echo $row["sub_id"]; ?>"><?php echo $row["sub_name"]; ?></option>
             <?php } ?>
             </select>
             </div>
             </div>
             <div class="form-group">
             <label class="col-sm-3 control-label">Duration:</label>
             <div class="col-sm-5">
             <input type="text" name="duration" class="form-control" id="duration">
             </div>
             </div>
             <div class="form-group">
             <label class="col-sm-3 control-label">Cost:</label>
             <div class="col-sm-5">
             <input type="text" name="cost" class="form-control" id="cost">
             </div>
             </div>
</div>

<script src="<?php echo $baseurl;?>assets/js/jquery.min.js"></script>
<script type="text/javascript">
    function get_values()
    {
        var sub_id=$("#subscription").val();

        $.post ("<?php echo $baseurl;?>index.php/admin/get_values/" + sub_id,

            function(data)
            {
                $('#duration').val(data.sub_duration);
                $('#cost').val(data.sub_cost);
            }
        );
    }
</script>

Now we will check controller functions, which are declared as below.

[wp-like-lock]

<?php
class admin extends CI_Controller
{
	function __construct()
	{
	    parent::__construct();
		 
		$this->load->model('admin_model');
	}
	
    public function add_fet_Subscription()
    {
    $data["add_fet_Subscription"]=$this->admin_model->fet_subscriptions();
        $this->load->view("subscription",$data);
    }
    public function get_values($sub_id)
    {
        $duration=$this->db->get_where ("tbl_subscriptions",array("sub_id"=>$sub_id));
        foreach ($duration->result()  as $row)
        {
            $arr = array('sub_duration' => $row->sub_duration, 'sub_cost' => $row->sub_cost);
            header('Content-Type: application/json');
            echo json_encode($arr);
        }
    }
}
?>

[/wp-like-lock]

when user selects the dropdown in subscription form ,onchange event will fire and get the values of sub_duration and sub_cost
Thanks for reading this article,for any queries please use comment system.