The aim of this tutorial is simply to get the data from a checkbox or checkboxes and store it in an MYSQL database and on page load, the data is retrieved from the database and is shown which would be suitable for an options page in a PHP script. This script has now been included in wow playground with new and updated features.


I am assuming that you have access to a PHP web server and have a MYSQL database. I highly recommend Xampp for your testing needs. I am also assuming that you know the basics on uploading and using an FTP program and have the use of a text editor or IDE.

Database Setup

When it comes to the database I like the simpler things in life so I use a MYSQL class from This will be included within the script in which you can download from the bottom of this post. Unzip the download file and upload it to your server. As you can see the checkbox_demo folder has 1 file and 1 folder containing 3 files. Inside the lib folder there is the Database.class.php, config.php and setup.php and in the root folder there is index.php. First and foremost you need to open config.php and put your database settings in the appropriate places and save.

Now navigate to lib/setup.php with your browser. Below is the source of the setup file to give you an understanding.


Setup.php is a basic form when the user clicks on setup button the script creates the table called options within your database, then creates the checkbox column which is of a type enum, a string that can only take 1 set of allowed values, in my case ‘Y’ or ‘N’. Then it adds data to the database, deletes the setup file and then re-directs you to index.php.

Checkboxes Demo

Now as for index.php which I have split into 3 parts. The opening lines connect, query and select the value of checkbox from the database. It stores the value of checkbox to the variable $checkbox and then it checks to see if the value of checkbox is ‘Y’ or ‘N’. If it is ‘Y’ then the variable $value is given a ‘Y’ and the variable $selected is then given ‘checked=”checked”‘. This is then passed on to the form which will tick the checkbox and set the value of the checkbox to ‘Y’.
The second part deals with the saving the data to the database. First it checks to see if the hidden field has been submitted then it checks to see if the form flowers value is ‘Y’ or ‘N’. if it is ‘Y’ the $value variable is set to ‘Y’ and then the database is updated accordingly with the correct values and the page is told to refresh.
As you can see in the 3rd part this is the basic form where the contents of checkbox are determined by the values that have come from the database or has been changed by user input.