[eluser]afro[/eluser]
I have fields in my webpage that i want to be
load from the database when one on the fields has been filled by the user using AJAX
The fields are as follows
Code:
StaffId............. StaffName................
Department.......... Leave Balance............
I want when the StaffId is filled by the user, the rest of the fields are auto filled.
The staff table is has as follows.
Code:
+--------|---------|-------|--------|----------|------------+
|StaffId |FirstName|Surname|LastName|Department|LeaveBalance|
+--------|---------|-------|--------|----------|------------+
I have tried out this but does not work totally so can some help me out
Code:
function get_staff_id(staffid) {
var p = {};
p[staffid] = staffid
$('#leaveaplication').find('#staffid').load(index.php/home/load_staff_data,p,function(str){
});
}
$(document).ready(function()
{
$('#leaveaplication').find('#staffid').blur(function()
{
alert "Hello world";
var staffid = $('#staffid').val();
$.post('/index.php/home/load_staff_data',
{ 'staffid':staffid },
function(result) {
$('#staffdetails').replaceWith('');
if (results) {
$('#staffid').after('<div id="bad_username" style="color:red;">' +
'<p>(That StaffId does not exits. Please type the correct Staff Id:)</p></div>');
}
}
);
});
});
Code:
public function load_staff_data()
{
$this->load->model('user_model', '', TRUE);
$staffid = trim($_POST['staffid']);
$results = $this->user_model->get_staff_data($staffid);
}
Code:
public function get_staff_data($staffid)
{
$this->db->select('StaffId,StaffName, Department, LeaveBalance');
$this->db->where('StaffId', $staffid);
$query = $this->db->get('staff');
$row = $query->row();
$results[record][$row->StaffName][StaffName] = $row->StaffName;
$results[record][$row->Department][Department] = $row->Department;
$results[record][$row->LeaveBalance][LeaveBalance] = $row->LeaveBalance;
return $results;
}