Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Select Object on Subform to Update fields

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
ernesttan1976
Posts: 51
Joined: Sat May 16, 2020 10:08 am

Select Object on Subform to Update fields

Unread post by ernesttan1976 »

Hi there, I need some help...

I need to base on the onchange event of the "Select" object, to trigger an SQL and pull out the fields "roles" and "name" and "group" from "employee" table.
And write the values in these subform fields.

I have been trying for sometime now, got no clue.

I tried using the Lookup object, however it is very slow, the select object is very fast.

Many thanks!

Ernest
You do not have the required permissions to view the files attached to this post.
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Select Object on Subform to Update fields

Unread post by kev1n »

Hi,


1. Put this code on the onchange event of the employee select field

Code: Select all

getEmployeeInfo(event);
employee_onchange.png
2. In your form's Custom Code, add this code:

Code: Select all

function fillEmployeeInfo(prefix, roles, name, group) {
  
    $('#' + prefix + 'roles').val(roles).change();  // replace 'roles' with your roles field object Id
    $('#' + prefix + 'name').val(name).change();  // replace 'name' with your name field object Id
    $('#' + prefix + 'group').val(group).change();  // replace 'group' with your group field object Id

}

function getEmployeeInfo(event) {

    var empId = $('#' + event.target.id).val();
    nuSetProperty('empId', empId);

    var prefix = $(event.target).attr('data-nu-prefix');
    nuSetProperty('prefix', prefix);

    nuRunPHPHidden('getEmployeeInfo', 0)
}
3. Create a PHP Procedure with the name getEmployeeInfo

Code: Select all

function getEmployeeInfo($employee_id) {
   
   // Change the SQL query here:
    $sql = "SELECT emp_roles, emp_name, emp_group FROM `employee` WHERE `employee_id` = ?";

    $qry = nuRunQuery($sql, [$employee_id]);
    $row = db_fetch_object($qry);

    return array(
        "roles" => $row->emp_roles,  // replace emp_roles with your sql column
        "name" => $row->emp_name, // replace emp_name with your sql column
        "group" => $row->emp_group, // replace emp_group with your sql column

    );
}


$empInfo = getEmployeeInfo("#empId#");

$roles = $empInfo["roles"];
$name = $empInfo["name"];
$group = $empInfo["group"];

$j = "fillEmployeeInfo('#prefix#', '$roles','$name','$group'); ";

nuJavascriptCallback($j);
php_procedure.png
You do not have the required permissions to view the files attached to this post.
Last edited by kev1n on Tue Oct 06, 2020 8:09 am, edited 1 time in total.
ernesttan1976
Posts: 51
Joined: Sat May 16, 2020 10:08 am

Re: Select Object on Subform to Update fields

Unread post by ernesttan1976 »

WOw, Kevin, Thank you so much. I will try it out and report back with good news :)
ernesttan1976
Posts: 51
Joined: Sat May 16, 2020 10:08 am

Re: Select Object on Subform to Update fields

Unread post by ernesttan1976 »

Woohoo! it works! :) Thank you Kevin :D
Post Reply