Can anyone help me why I can't pass the json data on the text field? I got this error when I open my modal: Uncaught ReferenceError: data is not defined. Thanks so much! :)
public function generateEmployeeId() {
$number = getTotalEmployees();
$employee_id = 'ACZ'. date("Y") . $number;
echo json_encode (['employee_id' => $employee_id]);
}
public function getTotalEmployees() {
$num = 1;
++$num;
$len = strlen($num);
for($i = $len; $i < 3; ++$i) {
$num = '0' . $num;
}
return $num;
}
Function to open modal and retrieve employee id
function modal_add_employee() {
$('#modal_employee').modal('show');
$('#data-submit--employee').html('Add Employee');
$('#modal_employee').find($('#staff_id')).val('');
$('#modal_employee').find($('#employee_lastname')).val('');
$('#modal_employee').find($('#employee_firstname')).val('');
$('#modal_employee').find($('#employee_middlename')).val('');
$('#modal_employee').find($('#employee_email')).val('');
$('#modal_employee').find($('#employee_id')).val(data.employee_id);
$('#modal_employee').find($('#employee_contact')).val('');
$('#modal_employee').find($('#employee_address')).val('');
$('#modal_employee').find($('#employee_birthdate')).val('');
$('#modal_employee').find($('#employee_gender')).val("Male");
}
My Answer:
Y-m-d') . '-' . str_pad($number + 1, 4, '0', STR_PAD_LEFT);_ return $employee_id;_ }_ // Get total number of employees_ public function getTotalEmployees() {_ $employees = Employee::all();_ return $employees->count();_ }_ // Controller method to pass data to view_ public function showEmployeeForm() {_ $employee_id = generateEmployeeId();_ return view('employee_form', compact('employee_id'));_ }_
// View file employee_form.blade.php_ _ __
Rate this post
4 of 5 based on 6050 votesComments