How to insert multiple value onkey change action? How to insert multiple value onkey change action? codeigniter codeigniter

How to insert multiple value onkey change action?


EDIT: Once you have got the array, simply json_encode() and send back to the ajax. There you can decode it back. You also need to use this in your ajax : dataType : 'json'

Also note that we are setting key 'error' in the php and returnig $data array, if there was an error and checking the value in the success() of ajax to deduce if there was an error or not.

Javascript: Note this is striped down version of what you will do, but it serves the purpose of what you need to do. Hope it helps.

$.ajax ( { method : 'POST',url : scriptUrl,data : data,    cache : false, processData: false, /** Don't process the data **/    contentType: false, /** Set content type to false as jQuery will tell the server its a query string request **/    dataType : 'json', /** we will return data in the key-value format from the php file **/    success : function ( data, textStatus, jqXHR ) { /** We can treat the returned data as object. **/        if ( typeof data.error === 'undefined' ) { /** damn error **/ }        else { /** We got our data. **/            for (var key in data) {                alert(key + " -> " + data[key]);            }        }    }

This should work for all your queries irrespective of number of records and number of columns.

$query = $this->db->get();/** Check for return value    If there is an error, set error key, we will use it in the ajax to    check if there was an error at the bancend  */if ( ! $query ) { $data['error'] = TRUE; return $data; }/** * Array to hold our data. * For reference look at the foreach() loop. */$data = array (  );/** Get each record as $row. **/foreach ( $query->result (  ) as $row ){    /**     * Temporary array to hold the data of this record.     */    $temp = array (  );    /** Get each value as $key => $value pair **/    foreach ( $row as $key => $value)    {        $temp [ $key ] = $value;    }        array_push ( $data, $temp );}return $data;

Now $data is an multidimensional associative arrays, $data[0] is equals to one record array ( 'id' =>1, 'name' => 'something'... );

$data = array ( array ( 'id' =>1, 'name' => 'something'... ), ( 'id' =>1, 'name' => 'something'... ),.... );


  1. On change in any value of the row fetch all the sibling values having same names
  2. Say if you changed the amount in row 4 inside on change function fetch the values of all the n rows and form an object such as

    var a = "[{ "key1"=>"value1", "key2"=>"value2", "key3"=>"value3", "key4"=>"value4" },{ "key1"=>"value1", "key2"=>"value2", "key3"=>"value3", "key4"=>"value4" },{ "key1"=>"value1", "key2"=>"value2", "key3"=>"value3", "key4"=>"value4" },]"

  3. and post this to PHP

  4. In PHP read this array and using foreach loop parse the array and insert (if it is a new record), update (if it was an existing record) and delete (all the record which are there already in database but not in the current array) each record in your database


To get multiple records for given value,you have to change your model code.Replace your code with following code in your model:

public function get_service_name($id){    $this->db->select('name');    $this->db->from('service');    $this->db->where('id', $id);    $query = $this->db->get();    $result = $query->result_array();    return $result; // It will provide you array of all service name having id=$id }