This is for bootsrap theme but shouldn't be dificult to change for other themes:
$("#id_client_field_box label").append(" <a onclick='javascript:addOption(\"/clients/addOption\",\"field-id_client\",\"Enter client name\");' class='btn default btn-sm pull-right '> new </a>");
function addOption(url,selectid,msg){ var value = prompt(msg); if (value != null){ $.post( url, {"value": value } , function() { }) .done(function(data) { data = $.parseJSON(data); if(data.status=="ok"){ $("#"+selectid).append("<option value='"+data.id+"'>"+value+"</option>"); $('#'+selectid).val(data.id); $('#'+selectid).trigger("liszt:updated"); }else{ alert( "error" ); } },'json') .fail(function() { alert( "error " ); }); } }
public function addOption(){ $data = array("name"=>$this->input->post("value")); $this->db->insert("client",$data); echo json_encode(array("id"=>$this->db->insert_id(),"status"=>"ok")); }