The AppGini Blog
A few tips and tricks to make your coding life a tiny bit better.

How to make a checkbox mandatory in AppGini signup form

I got a question from an AppGini user on how to make it a requirement for users to check an ‘I agree’ box when they sign up to AppGini — possibly as part of GDPR compliance, or to agree to some other terms and conditions.

We do plan to add support for this as part of future versions of AppGini. However, for now it can be done through a quick JavaScript code customization in hooks/footer-extras.php . The steps in brief are: check if the current page is the signup page, and if so, change custom field #4 to a checkbox, and prevent form submission if the box is unchecked.

The steps above make use of one of the custom user fields and turn it into a checkbox. In AppGini, the user profile supports storing up to 4 customizable fields. If you follow the steps here, one of them will be used for the checkbox, so you’ll have only 3 left. It’s a caveat but can be used as a work-around for now till we implement it as a separate feature in AppGini. So, the code to be placed in hooks/footer-extras.php would look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<script>
	$j(function() {
		// if this is not the signup page, don't continue
		if(! /membership_signup.php/.test(location.href)) return;

		// change custom4 field to a checkbox
		$j('#custom4')
			.attr('type', 'checkbox')
			.removeClass('form-control')
			.val(1)
			.wrap('<div class="checkbox"><label></label></div>')
			.after('I agree to <a href="hooks/terms.php">terms and conditions</a>');

		// make the checkbox mandatory
		$j('form').submit(function() {
			if(!$j('#custom4').prop('checked')) {
				alert('Sorry .. must agree to sign up!');
				$j('#custom4').focus();
				return false;
			}
		})
	})
</script>

You could change the wording of the checkbox by editing line 12 above. Also, you could change the error message by changing line 17. You’ll need to add a file containing the terms and conditions in the hooks folder, naming it terms.php. It might look something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php
	define('PREPEND_PATH', '../');
	$hooks_dir = dirname(__FILE__);
	include("{$hooks_dir}/../defaultLang.php");
	include("{$hooks_dir}/../language.php");
	include("{$hooks_dir}/../lib.php");
	
	include_once("{$hooks_dir}/../header.php");
?>

<div class="page-header"><h1>Terms and conditions</h1></div>

<ol>
	<li> Condition 1 here</li>
	<li> Condition 2 here</li>
	<li> More terms here</li>
</ol>
	
<?php include_once("{$hooks_dir}/../footer.php"); ?>

Change the wording above as you see fit. Please let me know if you have any questions or comments.


Archived Comments

Ron Williams

2019-05-07 at 1:27 pm

Hi Ahmed,

I made the changes as shown, all works as intended except: Once a new signup selects checkbox (on a group marked as automatically approve them) the app jumps to myurldirectory /admin/membership_thankyou.php?redir=1 instead of myurldirectory /membership_thankyou.php?redir=1

Cheers,

Genedy

2019-05-07 at 3:10 pm

I’ve received some similar reports recently and I’m investigating it .. Is your AppGini app hosted on the document root directly? For example, on example.com directly rather than example.com/app?

Ron Williams

2019-05-07 at 5:38 pm

Thanks for quick reply. Yes its directly mysite.com

Maybe nothing to do with it, but only real difference to my AG setup is I took out line as follows:

1
2
3
4
5
$dbServer = ‘db00000000.hosting-data.io’;
$dbUsername = ‘dbu00000000’;
$dbPassword = ‘00000000’;
$dbDatabase = ‘dbs00000000’;
// $appURI = ‘OCMS’;

Genedy

2019-05-07 at 11:43 pm

I assume the above are not actual username/password for your database as this is a public comment 🙂 Please try changing the commented line (after uncommenting it) to:

1
$appURI = '';

Ron Williams

2019-05-08 at 12:02 am

Perfect, works a treat.

LOL no not actual user/password.

Thanks Ahmed

Ron Williams

2019-05-07 at 5:46 pm

Hi Ahmed,

Sorry, just did a test on local PC where I leave in reference to

1
$appURI = ‘OCMS’; 

and it takes me to correct page, so it does seem to be related to this. Unfortunately online/server only works on my projects when I take out the $appURI line in config file, if left in it takes me to blank page.

Genedy

2019-05-07 at 11:52 pm

As I suggested above, try changing that to:

1
$appURI = '';

When moving your AppGini app from localhost or testing environment to production server, you should not upload config.php as this would differ between the 2 servers. Once uploaded, you should then see the setup page and perform the setup according to the server settings.

Ron Williams

2019-05-08 at 1:15 pm

Understood, I normally do it that way. However, sometimes I prepare the database locally inclusive groups, users, example records etc. and export/upload the entire sql to my server host with a revised config.php – So it was my error in config file. Thanks for sorting this out for me, much appreciated.

Genedy

2019-05-08 at 2:32 pm

You’re always welcome 🙂 I’m glad it works now.