Run query from javascript

Questions related to customising nuBuilder Forte with Javascript or PHP.

Run query from javascript

Unread postby gevli » Wed Apr 07, 2021 7:18 am

As a newbie with nuBuilder Forte, I created a form with a subform. The form contains a customer field, the subform contains rows with a zip-code, weight and price.
When the zipcode or the weight is changed, I need to fetch the matching price from the database (some matrix which defines the prices).

In the (javascript) onchange-event of the zip-code and weight fields, i have managed to fetch the values of the customerId, weight and zipCode.
Also I defined the query which has to be run and returns the price.

But I have no clue how to proceed from javascript to PHP and actually run this query and return the value so I can update the value field.
Any tips/suggestions on this? Or is there a better approach?
gevli
 
Posts: 4
Joined: Wed Apr 07, 2021 6:41 am
nuBuilder Version: 4.5

Re: Run query from javascript

Unread postby kev1n » Wed Apr 07, 2021 3:43 pm

Hi and welcome to nuBuilder !

Declare this function in your form's Custom Code. Replace sub_zip, sub_weight, sub_price with your column IDs.

Code: Select all
function subformChanged(event) {

   var id = event.target.id;
   zipVal = nuSubformRowObject(id, 'sub_zip').val();
   weightVal = nuSubformRowObject(id, 'sub_weight').val();
   priceId = nuSubformRowObject(id, 'sub_price').attr('id');
   nuRunPHPHiddenWithParams('get_price', 'param', {zip: zipVal, weight: weightVal, price_id: priceId}, 0);   
}


In the JS onchange-events of the zip-code and weight field, call:

Code: Select all
subformChanged(event);


Last but not least, create a (Tab Builders ->) Procedure with the Code get_price:

Code: Select all
$param = json_decode(base64_decode('#param#')); // retrieve the parameters

$zip = $param->zip;
$weight = $param->weight;
$priceId = $param->price_id;

// Retrieve the price here ...
// $price = .....

// Change the price:
$js = " $('#$priceId').val('$price').change(); ";
nuJavascriptCallback($js);


procedure.png
procedure.png (14.79 KiB) Viewed 42 times
kev1n
nuBuilder Team
 
Posts: 1625
Joined: Mon Oct 15, 2018 2:13 am
nuBuilder Version: 4.5

Re: Run query from javascript

Unread postby gevli » Thu Apr 08, 2021 6:14 am

Thanks, this helped me a lot.
gevli
 
Posts: 4
Joined: Wed Apr 07, 2021 6:41 am
nuBuilder Version: 4.5

Re: Run query from javascript

Unread postby kev1n » Thu Apr 08, 2021 3:35 pm

You're welcome!
kev1n
nuBuilder Team
 
Posts: 1625
Joined: Mon Oct 15, 2018 2:13 am
nuBuilder Version: 4.5


Return to Custom Code

Who is online

Users browsing this forum: No registered users and 6 guests

cron