Getting Jquery to call Function In Hooks file

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
mrunne
Posts: 4
Joined: 2018-11-13 16:39

Getting Jquery to call Function In Hooks file

Post by mrunne » 2018-11-13 22:26

I have the following sql update working in my after_update function in my hooks php file.
/* This will move all equipment to users new desk and add all equipment at desk */
$id = $data['selectedID'];
$newSeat = $data['CubeNum'] ;
if ($id == '557'){
}
else{
sql("UPDATE `pnb_equip` SET `Location` = $newSeat WHERE `Assigned`= $id ", $eo);
sql("UPDATE `pnb_equip` SET `Assigned` = $id WHERE `Location`= $newSeat ", $eo);
}

This code works perfectly, except for the fact that I don't want it to run every time I update a Detail View record. With that being said, I created a Jquery script below that places a button on the detail view and it is working as expected. I know that by the working text alerts.
<script>
$j(function(){
<?php if($selectedID){ ?>
$j('#pnb_user_dv_action_buttons .btn-toolbar').append(
'<div class="btn-group-vertical btn-group-lg" style="width: 100%;">' +
'<button type="button" class="btn btn-warning btn-lg" onclick="moveEquipment()">' +
'<i class="glyphicon glyphicon-ok"></i> Move Equipment </button>' +
'</div>'
);
<?php } ?>
});
function moveequipment(){
var txt;
var r = confirm("You are moving all equipment to users current Seat");
if (r == true) {
alert('Completing Move');
}
else {
alert("Canceled")
}
}
</script>
The issue is how do I make the sql changes work by using the confirm popups?
Mr. Runne
Dev Engineer

pböttcher
AppGini Super Hero
AppGini Super Hero
Posts: 518
Joined: 2018-04-01 10:12

Re: Getting Jquery to call Function In Hooks file

Post by pböttcher » 2018-11-14 07:37

Hi,

if you want to use jquery you would need to add an ajax call to a php file which would contain your SQL statement to be executed. You cannot directly call a function (not without additional work).

Just another idea:
Why dont you add a checkbox field that would indicate that you want to update your database with your mentioned SQL queries. In the after_update function you could react on the value of the checkbox and decide to run your SQL or not.

mrunne
Posts: 4
Joined: 2018-11-13 16:39

Re: Getting Jquery to call Function In Hooks file

Post by mrunne » 2019-04-30 21:55

Changed the button to a checkbox, but still unable to do the Move equipment sql statements

<?php if($selectedID){ ?>
$j('#pnb_user_dv_action_buttons .btn-toolbar').append(
'<div class="form-check">' +
'<input type="checkbox" class="form-check" id="exampleCheck1" >' +
'<label class="form-check-label" for="exampleCheck1">Move All Equipment too?</label>'
);

<?php } ?>
});


is added to the dv function

And in the after_update

if (!empty($_GET['exampleCheck1'])) {
/* This will move all equipment to users new desk and add all equipment at desk */
$id = $data['selectedID'];
$newSeat = $data['CubeNum'] ;
if ($id == '557'){
}
else{
sql("UPDATE `pnb_equip` SET `Location` = $newSeat WHERE `Assigned`= $id ", $eo);
sql("UPDATE `pnb_equip` SET `Assigned` = $id WHERE `Location`= $newSeat ", $eo);
}


any help would be great
Mr. Runne
Dev Engineer

pböttcher
AppGini Super Hero
AppGini Super Hero
Posts: 518
Joined: 2018-04-01 10:12

Re: Getting Jquery to call Function In Hooks file

Post by pböttcher » 2019-05-01 18:05

Hi,

you added a checkbox to the DV, but this checkbox is on the page only. It has no reference to the data you are submitting once you click on the save button.
The idea with the checkbox was, that you add it in AppGini to you database and use it once you press save and get that value.

i.e. you add the field move_equiment to your table and make it hidden on the TV and the DV .

Then you can use the code above and add

Code: Select all

$j('#exampleCheck1').on('change', function () { $j('#move_equiment').val($j('#exampleCheck1')[0].checked); });

this will write true or false into your field move_equiment.

in the after_update function you can check for this value and act accordingly.
Like:

Code: Select all

if ($data['move_equiment'] == 'true') {

$id = $data['selectedID'];
$newSeat = $data['CubeNum'] ;
if ($id == '557'){
}
else{
sql("UPDATE `pnb_equip` SET `Location` = $newSeat WHERE `Assigned`= $id ", $eo);
sql("UPDATE `pnb_equip` SET `Assigned` = $id WHERE `Location`= $newSeat ", $eo);

// set the fieldvalue back to false for the next update !!
sqlvalue("UPDATE pnb_user set move_equiment='false' where id =".$id");
}
}
Watch that in this case true and false are strings!

Hope that helps

mrunne
Posts: 4
Joined: 2018-11-13 16:39

Re: Getting Jquery to call Function In Hooks file

Post by mrunne » 2019-05-01 19:27

Is that the only way by updating the table in the DB? I really just want an easier way of referencing the if/then statement in the after_update code.
Mr. Runne
Dev Engineer

pböttcher
AppGini Super Hero
AppGini Super Hero
Posts: 518
Joined: 2018-04-01 10:12

Re: Getting Jquery to call Function In Hooks file

Post by pböttcher » 2019-05-01 20:54

Well the other option would be to use ajax. But this would always happen bevor the save has really executed, so this might not be what you want.

You could initiate an ajax call once the user checks the checkbox and run your SQL, but what happens if the user does not store the data and aborts the editing?

mrunne
Posts: 4
Joined: 2018-11-13 16:39

Re: Getting Jquery to call Function In Hooks file

Post by mrunne » 2019-05-02 15:16

Nothing should happen at that point. The update should only happen if the checkbox is checked and the submit button is clicked. Could an ajax call work under those conditions?
Mr. Runne
Dev Engineer

pböttcher
AppGini Super Hero
AppGini Super Hero
Posts: 518
Joined: 2018-04-01 10:12

Re: Getting Jquery to call Function In Hooks file

Post by pböttcher » 2019-05-02 17:52

Nope, you would have to create some logic to handle the ajax call in combination with the after_update hook and all possible option that a user could do.
I would go for the other option.

Post Reply