⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Simple add option on select input type



titu
  • profile picture
  • Member

Posted 23 February 2016 - 21:21 PM

This is for bootsrap theme but shouldn't be dificult to change for other themes:

$("#id_client_field_box label").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <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"));
	}