Welcome to the nuBuilder Forums!

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

Run query from javascript

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
gevli
Posts: 4
Joined: Tue Apr 06, 2021 11:11 pm

Run query from javascript

Unread post 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?
kev1n
nuBuilder Team
Posts: 4299
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Run query from javascript

Unread post 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
You do not have the required permissions to view the files attached to this post.
gevli
Posts: 4
Joined: Tue Apr 06, 2021 11:11 pm

Re: Run query from javascript

Unread post by gevli »

Thanks, this helped me a lot.
kev1n
nuBuilder Team
Posts: 4299
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Run query from javascript

Unread post by kev1n »

You're welcome!
Post Reply