Set/Save field value via Button

This sub-forum is for discussing all topics related to AppGini Helper JavaScript Library, provided by bizzworxx as a third-party AppGini plugin.
Post Reply
sgrzy01
Posts: 16
Joined: 2016-07-25 20:04

Set/Save field value via Button

Post by sgrzy01 » 2021-02-01 21:52

Though I can be dangerous with my PHP, when it comes to JavaScript, I am far from competent.

The tools have several examples of a button in DV and TV to execute some JavaScript, but the examples just get values...

The problem I've trying to solve (and not very successfully) is my users want, in the table view, to hit a button and "close" the record, ie: set and save the "closed_date" field with today's date w/o opening the record, and refresh the TV to see new status.

If that is not possible, once the record in opened in DV, hit an action button and again, set and save the "closed_date" field and go back to TV.

Any examples or guidance would be appreciated.

Thanks!.

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Set/Save field value via Button

Post by onoehring » 2021-02-02 07:10

Hi,

see my footer, how to insert a new button (2nd save button). You can also use AppGini Helper (viewforum.php?f=13).
The button should have a SUBMIT type and you can simpy add a

Code: Select all

?setDate=1
to it. Once the form has been submitted, check in the hooks/tablename.php -> _init function, if the GET parameter "setDate" is set and if so, see if it has the value 1. If the setDate is equal to 1, then write some php code (in the before_update function) to add the current date to a field.
If the field is read-only or hidden, the variable $data['yourFieldname'] is not available. In this case you can write the date in the after_insert (and after_udpate) function.

Well, no code, just guidance ;-)

Olaf

sgrzy01
Posts: 16
Joined: 2016-07-25 20:04

Re: Set/Save field value via Button

Post by sgrzy01 » 2021-02-02 08:28

Thank you! Definitely gives me something to work with... !

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Set/Save field value via Button

Post by jsetzer » 2021-02-02 08:36

Hi,

if I understood your question right, it was about adding a button to table view and, on click (clientside), changing a database value on serverside using PHP. Here is how you can do using AppGiniHelper Javascript Library functions. I have inserted many comments for better understanding for beginners.

You are familiar with PHP, so I'm not showing the complete PHP code for the second part (serverside part) but just naming the relevant steps for server side.


Part 1: Javascript (clientside)

Code: Select all

// file: hooks/TABLENAME-tv.js

// change the PHP filename
// you can use placeholder %ID% in your url
var url = "tasks_close.php?SelectedID=%ID%";

// wait for document ready due to lazy loading of table
jQuery(document).ready(function () {

    var tv = AppGiniHelper.tv;

    // we need some space for the button. 
    // Change width of first column to 150px
    tv.setWidth(0, 150);

    // add a button to TV table on EVERY row
    tv.addLink(url, "ok", "Close Record");
    // 2nd parameter: glyphicon name
    // 3rd parameter: button text
});
chrome_JSVcqcXcCB.png
chrome_JSVcqcXcCB.png (13.66 KiB) Viewed 2068 times

On mouseover check the URL of each button. The placeholder %ID% has been replaced by the primary key per row.

chrome_Nir4bv1eM4.png
chrome_Nir4bv1eM4.png (3.82 KiB) Viewed 2068 times


Part 2: PHP (serverside)

Code: Select all

<?php
// file: tasks_close.php
include("language.php");
include("lib.php");

// get SelectedID parameter from $_REQUEST
// check edit-permissions for that specific record and current user
// do your database changes using sql("YOUR SQL COMMAND", $eo);
// redirect to table view or detail view using PHP redirect("..."); function
// error handling
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

sgrzy01
Posts: 16
Joined: 2016-07-25 20:04

Re: Set/Save field value via Button

Post by sgrzy01 » 2021-02-02 15:05

Perfect!

I was already doing something similar with an external function, didn't make the connection to do the same thing to update the record...

Thank you again for making me think about this correctly.

Image

Post Reply