Welcome to the nuBuilder Forums!

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

Trying to run a procedure from a button Topic is solved

Questions related to customising nuBuilder Forte with JavaScript or PHP.
tsignadze
Posts: 43
Joined: Tue Feb 21, 2023 12:14 am
Has thanked: 22 times
Been thanked: 1 time

Trying to run a procedure from a button

Unread post by tsignadze »

Hello everyone!
Forgive me if my question is somewhat stupid, I am very new to this.

I have a form where I have 3 fields : TerminalID, old_address, new_address.
form.png
When user inputs TerminalID old_address is populated automatically from database. Then user enters new_address and I want to run a following query:

Code: Select all

UPDATE db SET address = #new_address# WHERE TerminalID = #TerminalID#;
How do I attach my button to a procedure which will run up mentioned query?
You do not have the required permissions to view the files attached to this post.
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: Trying to run a procedure from a button

Unread post by kev1n »

Hi,

Create a Procedure in which you run the query with the nuRunQuery() function.
Add a run button to your form and choose the procedure to run.

I've created a small demo:

https://streamable.com/s8nexa
tsignadze
Posts: 43
Joined: Tue Feb 21, 2023 12:14 am
Has thanked: 22 times
Been thanked: 1 time

Re: Trying to run a procedure from a button

Unread post by tsignadze »

Kev1n, you are the man!

I am too stupid, I have created mysql procedures and was trying to pass variables from php procedures. This was dead easy :D

Now please help me to add a notification that query was successful and close this popup form.

Once I learn how to get my head around, I will not ask this questions I promise :thumb:
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: Trying to run a procedure from a button

Unread post by kev1n »

Add this PHP in your procedure. This will display the message for 2000 ms (2 seconds) and then closes the popup:

Code: Select all

$js = " 

nuMessage('Update was successful!', 2000, closePopup); 

function closePopup() {
    nuClosePopup();
}

";
nuJavaScriptCallback($js);
PS: Please feel free to ask as many questions as you like. I am here to help you and provide information to the best of my ability.
tsignadze
Posts: 43
Joined: Tue Feb 21, 2023 12:14 am
Has thanked: 22 times
Been thanked: 1 time

Re: Trying to run a procedure from a button

Unread post by tsignadze »

Thanks for a quick reply.
Message was displayed but popup did not close, do I need to throw forms id somewhere?

Also, forgot to mention, I want to check if query was successful. I have found this, but can not implement:

Code: Select all

if ($DB->Execute($sql) === false) {

print $DB->ErrorMsg();

}

// If query executed

if ($DB->affected_rows() !== 0) { //true }
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: Trying to run a procedure from a button

Unread post by kev1n »

My mistake, it is:

Code: Select all

nuClosePopup();
tsignadze
Posts: 43
Joined: Tue Feb 21, 2023 12:14 am
Has thanked: 22 times
Been thanked: 1 time

Re: Trying to run a procedure from a button

Unread post by tsignadze »

Got it working, please help me implement two new validation points and I will clone this for other buttons for now.

1) I need to check if entered terminal id is existing in my database, if not, display "Check terminal id".
2) Do not allow empty field for new_address, if it is left blank, display "New Address can not be left blank"
3) If query was not successful for whatever reason, display error.
4) (Done) if query was successful display "Update was successful";

Once I will be able to implement this I can create 70% of my access db in nubuilder :mrgreen:
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: Trying to run a procedure from a button

Unread post by kev1n »

To get the affected rows with PHP PDO (this is what nuBuilder uses), call db_num_rows() to return the affected rows:

Code: Select all

$stmt = nuRunQuery("UPDATE db SET address = #new_address# WHERE TerminalID = #TerminalID#;");

if (db_num_rows($stmt) == 1) { // 1 row updated

	$js = " 
	nuMessage('Update was successful!', 2000, closePopup); 

	function closePopup() {
		nuClosePopup();
	}
	";

} else {
	$js = " nuMessage('No rows updated'); ";
}

nuJavaScriptCallback($js);
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: Trying to run a procedure from a button

Unread post by kev1n »

Updated code:

Code: Select all

if ('#new_address#' == '') {
    nuDisplayError('New Address can not be left blank');
    return;
}

$result = nuRunQuery("UPDATE db SET address = #new_address# WHERE TerminalID = #TerminalID#;");

if (db_num_rows($result) == 1) { // 1 row updated
    $js = " 
	nuMessage('Update was successful!', 2000, closePopup); 

	function closePopup() {
		nuClosePopup();
	}
	";

}
elseif ($result == - 1) {
    nuDisplayError('Update error');
    return;
}
else {
    $js = " nuMessage('No rows updated'); ";
}

nuJavaScriptCallback($js);

tsignadze
Posts: 43
Joined: Tue Feb 21, 2023 12:14 am
Has thanked: 22 times
Been thanked: 1 time

Re: Trying to run a procedure from a button

Unread post by tsignadze »

You made what seemed to be a very demanding task very easy for me!!

Thank you!
Post Reply