show a button only if a checkbox is checked

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

show a button only if a checkbox is checked

Post by fgazza » 2019-10-17 08:37

Hello to all.
I created this code to make a custom button appear or disappear.
The button should appear only if the checkbox "aautorizza_attestato" is checked.
The problem is that with this code, if the autorizza_attestato field is checked, the button does not appear unless I uncheck and then check again the field each time I enter the detailed record view.
This is not good because only the administrator can modify the field while users should find or not find the button depending on whether the administrator has set it to checked or unchecked.

Here is my code. Thanks for your help!

Fabiano

function partecipazione_evento_pubblico_dv($selectedID, $memberInfo, &$html, &$args){

/* if this is the print preview, don't modify the detail view */
if(isset($_REQUEST['dvprint_x'])) return;

ob_start(); ?>

<script>
$j(function(){
<?php if($selectedID){ ?>
$j('#partecipazione_evento_pubblico_dv_action_buttons .btn-toolbar').append(
'<div class="btn-group-vertical btn-group-lg" style="width: 100%;">' +
'<button type="button" class="btn btn-info btn-lg hidden" id="autorizza" onclick="stampa_attestato()">' +
'<i class="glyphicon glyphicon-print"></i> Stampa Attestato</button>' +
'</div>'
);
<?php } ?>

$j("#autorizza_attestato").click(function() {
if( $j('#autorizza_attestato').is(':checked') ) {
$j('#autorizza').removeClass('hidden');
} else {
$j('#autorizza').addClass('hidden');
}
});

});

function stampa_attestato(){
var selectedID = '<?php echo urlencode($selectedID); ?>';
window.location = 'attestato_evento_pubblico.php?pri_key=' + selectedID;
}

</script>

<?php
$form_code = ob_get_contents();
ob_end_clean();

$html .= $form_code;


if(!in_array($memberInfo['group'], ['Admins'])) {
ob_start();
?>
<script>
$j(function() {
$j('#autorizza_attestato').attr("disabled", true);
})
</script>
<?php
$html .= ob_get_clean();
}

}

fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

Re: show a button only if a checkbox is checked

Post by fgazza » 2019-10-17 16:00

Perhaps to make the code work as I would like the condition must look if the value in the field "autorizza_attestato" is equal to "1" (which is the value corresponding to the checked condition)?
I just don't know how to write the code.

Can someone help one time again to definitively solve the issue?

<script>
$j(function(){
<?php if($selectedID){ ?>
$j('#partecipazione_evento_pubblico_dv_action_buttons .btn-toolbar').append(
'<div class="btn-group-vertical btn-group-lg" style="width: 100%;">' +
'<button type="button" class="btn btn-info btn-lg hidden" id="autorizza" onclick="stampa_attestato()">' +
'<i class="glyphicon glyphicon-print"></i> Stampa Attestato</button>' +
'</div>'
);
<?php } ?>

$j("#autorizza_attestato").click(function() {
if( $j('#autorizza_attestato').is(':checked') ) {
$j('#autorizza').removeClass('hidden');
} else {
$j('#autorizza').addClass('hidden');
}
});

});

function stampa_attestato(){
var selectedID = '<?php echo urlencode($selectedID); ?>';
window.location = 'attestato_evento_pubblico.php?pri_key=' + selectedID;
}

</script>

mghielmi
Posts: 10
Joined: 2019-01-08 01:27

Re: show a button only if a checkbox is checked

Post by mghielmi » 2019-10-19 16:45

Check here: https://jsfiddle.net/lupet/jkcquzwb/11/
fgazza wrote:
2019-10-17 16:00
Perhaps to make the code work as I would like the condition must look if the value in the field "autorizza_attestato" is equal to "1" (which is the value corresponding to the checked condition)?
I just don't know how to write the code.

Can someone help one time again to definitively solve the issue.

fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

Re: show a button only if a checkbox is checked

Post by fgazza » 2019-10-21 12:38

Hello and thanks for the reply.
I believe there is a misunderstanding:
my problem is this:
an administrator puts the check in the field "authorize_attestato" so that the users with the validated field (checked) can then click on the "stampa attestato" ("print certificate") button.
With my code, and I also believe with the code that suggested the forum member mghielmi if the field is set to checked by the administrator and a user views that record, the button "Stampa Attestato" does not appear unless the user sets the field in uncheck and then again in checked mode. This is not good because the administrator decides whether the user can print his certificate or not (and therefore if the user can see the pusante "certified print") and for the user the checbox field "autorizza_attestato" (authorize certificate) is disabled!

I tried to put this condition in my code but it doesn't work (see part of the code in bold and big characters):

<script>
$j(function(){
<?php if($selectedID){ ?>
$j('#partecipazione_evento_pubblico_dv_action_buttons .btn-toolbar').append(
'<div class="btn-group-vertical btn-group-lg" style="width: 100%;">' +
'<button type="button" class="btn btn-info btn-lg hidden" id="autorizza" onclick="stampa_attestato()">' +
'<i class="glyphicon glyphicon-print"></i> Stampa Attestato</button>' +
'</div>'
);
<?php } ?>

$j("#autorizza_attestato").click(function() {
if ($data['autorizza_attestato'] == '1') {
$j('#autorizza').removeClass('hidden');
} else {
$j('#autorizza').addClass('hidden');
}
});

});

function stampa_attestato(){
var selectedID = '<?php echo urlencode($selectedID); ?>';
window.location = 'attestato_evento_pubblico.php?pri_key=' + selectedID;
}

</script>

I hope you can help me!

THANKS!

fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

HELP ME PLEASE! show a button only if a checkbox is checked

Post by fgazza » 2019-10-23 14:16

Hello everyone!

I regret going back to writing but unfortunately I can't solve this problem:

I have this script to display a custom button that link to a custom page:

function partecipazione_evento_pubblico_dv($selectedID, $memberInfo, &$html, &$args){

/* if this is the print preview, don't modify the detail view */
if(isset($_REQUEST['dvprint_x'])) return;

ob_start(); ?>

<script>
$j(function(){
<?php if($selectedID){ ?>
$j('#partecipazione_evento_pubblico_dv_action_buttons .btn-toolbar').append(
'<div class="btn-group-vertical btn-group-lg" style="width: 100%;">' +
'<button type="button" class="btn btn-info btn-lg" onclick="stampa_attestato()">' +
'<i class="glyphicon glyphicon-print"></i> Stampa Attestato</button>' +
'</div>'
);
<?php } ?>
});

function stampa_attestato(){
var selectedID = '<?php echo urlencode($selectedID); ?>';
window.location = 'attestato_evento_pubblico.php?pri_key=' + selectedID;
}

</script>

<?php
$form_code = ob_get_contents();
ob_end_clean();

$html .= $form_code;

}

I would like this script to be executed only in the detailed view of the records in which the checkbox "autorizza_attestato" is set to checked (i think that checked mean that in the database the value is 1).

the field "autorizza_atestato" is disabled for all users except for the administrator. So if the administrator validated the field by setting it to checked, then the user viewing the record in detail view can see and click on the button.

Can anyone help me by indicating the code to enter?

THANKS!

Fabiano

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: show a button only if a checkbox is checked

Post by pbottcher » 2019-10-23 21:45

Hi,

add a query to retrieve the value of the checkbox-element from the database for this record.
If the value is !=1 execute the statements beginning with the ob_start() till the $html .= $form_code;
else skip all this.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

Re: show a button only if a checkbox is checked

Post by fgazza » 2019-10-24 08:56

Thanks for your suggestions.
I tried to rewrite the code with your instructions but it doesn't work. Obviously something is wrong ... or a lot!
Can you help me again?
Thanks!
Fabiano

here is my code (you will see two version both failing):

/*retrieve value of the check-box element (first attempt - failed!! */
/* $selectedID = makeSafe($data['selectedID']);
$checkbox = sql("select autorizza_attestato from partecipazione_evento_pubblico where pri_key='{$selectedID}'", $eo);*/


/*retrieve value of the check-box element (second attempt - failed!! */
$selectedID = makeSafe($data['selectedID']);
$checkbox = sqlValue("SELECT autorizza_attestato FROM partecipazione_evento_pubblico where pri_key='{$selectedID}'"
);

if ($checkbox ==1) {

ob_start(); ?>

<script>
$j(function(){
<?php if($selectedID){ ?>
$j('#partecipazione_evento_pubblico_dv_action_buttons .btn-toolbar').append(
'<div class="btn-group-vertical btn-group-lg" style="width: 100%;">' +
'<button type="button" class="btn btn-info btn-lg" onclick="stampa_attestato()">' +
'<i class="glyphicon glyphicon-print"></i> Stampa Attestato</button>' +
'</div>'
);
<?php } ?>
});

function stampa_attestato(){
var selectedID = '<?php echo urlencode($selectedID); ?>';
window.location = 'attestato_evento_pubblico.php?pri_key=' + selectedID;
}

</script>

<?php
$form_code = ob_get_contents();
ob_end_clean();

$html .= $form_code;

}
Thank you!
Fabiano

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: show a button only if a checkbox is checked

Post by pbottcher » 2019-10-24 09:41

Hi,

just dont use
$selectedID = makeSafe($data['selectedID']);

use only

$checkbox = sqlValue("SELECT autorizza_attestato FROM partecipazione_evento_pubblico where pri_key='{$selectedID}'");
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

Re: show a button only if a checkbox is checked

Post by fgazza » 2019-10-24 12:16

Thank you so much!
Finally here is my code (I post it for other users) and it works!!!

function table_name_dv($selectedID, $memberInfo, &$html, &$args){

/* if this is the print preview, don't modify the detail view */
if(isset($_REQUEST['dvprint_x'])) return;



/*retrieve value of the check-box element */
$checkbox = sqlValue("SELECT check_box_field_name FROM table_name where pri_key='{$selectedID}'"
);

if ($checkbox ==1) {

ob_start(); ?>

<script>
$j(function(){
<?php if($selectedID){ ?>
$j('#table_name_dv_action_buttons .btn-toolbar').append(
'<div class="btn-group-vertical btn-group-lg" style="width: 100%;">' +
'<button type="button" class="btn btn-info btn-lg" onclick="print_certificate()">' +
'<i class="glyphicon glyphicon-print"></i> Print Certificate</button>' +
'</div>'
);
<?php } ?>
});

function print_certificate(){
var selectedID = '<?php echo urlencode($selectedID); ?>';
window.location = 'destination_page.php?pri_key=' + selectedID;
}

</script>

<?php
$form_code = ob_get_contents();
ob_end_clean();

$html .= $form_code;

}

Thanks!

Fabiano

Post Reply