1

I am new to CodeIgniter, so I dont know how to do this. I want to display values dynamically in a select box and after selecting the value it displays a textbox and then it then pass the textbox value and and the option( the names which is displayed on dropdown list) id to controller,so briefly what I want to do:

  • dynamically show the values in select box
  • after selecting the value dynamically create textBox
  • passing the selected or track the 'id' of dropdown list and textbox value to controller

here is my Model

function getAllCategories(){
    $this->db->select('cat_name');
    $q = $this->db->get('category');

    if ($q->num_rows() > 0){
        foreach($q->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }

}

my controller

function showCategoryNames(){
    $data = array();
    $this->load->model('categoryModel');
    $query = $this->categoryModel->getAllCategories();
    if ($query){
        $data['records'] = $query;  
    }    
    $this->load->view('itemsView',$data);   
 }

View: this is showing the simple list

<?php if(isset($records)) : foreach($records as $row) :?>
    <h2><?php echo $row->cat_name; ?></h2>
    <?php endforeach;?>
    <?php else :
endif;?>

3 Answers 3

3

how about

<select name="mySelect">
<?php foreach($records as $row) { ?>
<option value="<?=$row->id?>"><?=$row->cat_name?></option>
<?php } ?>
</select>

in your view?

Here is a tutorial about working with jQuery, Ajax and Codeigniter:

http://www.jotorres.com/2012/01/using-jquery-and-ajax-with-codeigniter/

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

2 Comments

yeah thanks it works .. now i want to implement the onchange function and without refreshing it creates a text box by using ajax or jquery
than you've to build a controller function which returns the data you need, you can use jQuery.Post() to do your request and build the textbox dynamicly. I've added a tutorial on my answer. But i think you should use a view and not inline HTML like in the Tutorial.
0

after loading form helper class, your view should be for creating dropdown

form_dropdown('size', $data_array, 'large');

Comments

0
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Trip_model extends CI_Model{

    var $table = 'tbl_trip';

    public function __construct(){
        parent::__construct();
        $this->load->database();
    }

    public function get_all_trips(){
        $this->db->from('tbl_trip');
        $query=$this->db->get();
        return $query->result();
    }

    public function get_by_id($id){
        $this->db->from($this->table);
        $this->db->where('trip_id',$id);
        $query = $this->db->get();
        return $query->row();
    }

    public function trip_add($data){
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    public function trip_update($where, $data){
        $this->db->update($this->table, $data, $where);
        return $this->db->affected_rows();
    }

    public function delete_by_id($id){
        $this->db->where('trip_id', $id);
        $this->db->delete($this->table);
    }
}

1 Comment

Just like at the other place where you've posted such code without explanation: how does this solve the original question? How does it populate data for a select field? Why do you need a delete_by_id method for 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.