I want to pre fill fields of the form on load with existing data from database. On page load i am making an ajax call which queries the data and assign the returned data to a knockout observable array.
client.js:
function clientModel()
{
var self = this;
this.lastTenClients = ko.observableArray();
this.pendingClients = ko.observableArray();
this.foundCustomerResult = ko.observable();
this.shouldShowCustomerMessage = ko.observable(false);
this.foundCustomers = ko.observableArray();
var base_url = window.location.origin;
var pathArray = window.location.pathname.split( '/' );
if(base_url === "http://localhost"){
var url = base_url+"/"+pathArray[1]+"/client/";
} else {
var url = base_url+"/client/";
}
$.ajax({
url: url+"get_last_ten_clients",
type: "get",
cache: false,
success: function(client_list) {
var data = $.parseJSON(client_list);
self.lastTenClients(data);
}
});
$.ajax({
url: url+"get_pending_data_clients",
type: "get",
cache: false,
success: function(client_list) {
var data = $.parseJSON(client_list);
self.pendingClients(data);
}
});
this.search_client = function()
{
self.shouldShowCustomerMessage(false);
self.foundCustomers.removeAll();
crsf = $("input[name=csrf_test_name]").val();
dataString = $("#search_client_input").val();
$.ajax({
url: url+"search_client_database",
type: "post",
cache: false,
data: {csrf_test_name: crsf, search_client_input: dataString},
success: function(customer_details) {
var data = (customer_details);
self.foundCustomers(data);
},
error: function(xhr, ajaxOptions, thrownError)
{
alert(xhr.status);
alert(thrownError);
}
});
}
}
ko.applyBindings(new clientModel());
Sample data in observable after ajax call:
foundCustomers {"id":"1","nameMusicCompany":"Company","natureMusicCompany":"Music"}
In the view i am trying to assign the value using textInput binding like following:
<input type="text" class="form-control input-sm" name="nameMusicCompany" id="nameMusicCompany" placeholder="Name of the Music Company"
data-bind="textInput: nameMusicCompany">
But instead of showing the value "company" it displays [object HTMLInputElement] inside the input box.
Controller:
public function search_client_database()
{
if(!empty($this->input->post('search_client_input')))
{
$result = $this->client_model->get_client_data($this->input->post('search_client_input'));
echo json_encode($result);
}
}
Model:
public function get_client_data($client_name)
{
$client_name = strtoupper($client_name);
$sql = "SELECT * FROM client_data where UPPER(nameMusicCompany) = ?";
$query = $this->db->query($sql, array($client_name));
if($query->num_rows() > 0)
{
return $query->row();
}
return false;
}