Page 1 of 1

Run query from javascript

Posted: Tue Apr 06, 2021 11:48 pm
by gevli
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?

Re: Run query from javascript

Posted: Wed Apr 07, 2021 8:13 am
by kev1n
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

Re: Run query from javascript

Posted: Wed Apr 07, 2021 10:44 pm
by gevli
Thanks, this helped me a lot.

Re: Run query from javascript

Posted: Thu Apr 08, 2021 8:05 am
by kev1n
You're welcome!