JDT

 

FREE Downloads
Articles
Tutorials

 

Working with the Checkbox on HTML Forms


Checkboxes are a common component found on many HTML forms, but how can you use them to interact with a MySQL database?

Setting up the database field

In order to allow the checking (selecting) or unchecking (deselecting) of a checkbox item to be recorded in a database, you need to:

a) have a database
b) have a table in that database, and
c) have a field in the table that corresponds to the checkbox.

The following file, which we've called create_field.sql, could be used to create a database called 'test', a table in that database called 'formfields', and a field (in the table) called 'checkbox_form_item'.

create_field.sql

CREATE DATABASE test;
use test;
create table formfields
(
checkbox_form_item char(1) default 'Y'
);

The idea here is that the database field will be set to either 'Y' (to indicate that the checkbox is selected), or 'N' (to indicate that the check is deselected). By default, in other words, the first time the HTML form containing the checkbox is accessed, the checkbox status is set to 'Y' (it is checked).

Creating the form

In order to be able to read from and write to the MySQL database table, we'll use a PHP script (form.php), which could typically contain code like that below to connect to the databse table:

form.php

<?php

mysql_connect("hostname", "username", "password");

$result = @mysql_query("select checkbox_form_item from test.formfields ");

while ($row = @mysql_fetch_assoc($result)){

$checkbox_form_item = $row['checkbox_form_item'];
}

print "<form name=\"form1\" method=\"post\" action=\"updateDatabase.php\">\n";

print "<p><input type=\"checkbox\" name=\"checkbox_form_item\"\n";
if ($checkbox_form_item==Y){
print "checked\n";
}
print ">This is a checkbox</p>\n";

print "<p><input type=\"submit\" name=\"Submit\" value=\"Update Database\"></p>\n"

print "</form>\n";

mysql_close();

?>

Having connected to the database and extracted the data from the 'checkbox_form_item' field (either 'Y' or 'N'), the script carries out a test to see if the data is 'Y', and if it is, checks (selects) the check box.

When the user clicks the 'Update Database' button, the form data is passed to the form handler (updateDatabase.php).

updateDatabase.php

<?php

$checkbox_form_item=$_POST['checkbox_form_item'];

if ($checkbox_form_item==on) {
$checkbox_form_item="Y";
}

else {
$checkbox_form_item="N";
}

mysql_connect("hostname", "username", "password");

$result = @mysql_query("update test.formfields set checkbox_form_item='$checkbox_form_item'");

mysql_close();

?>

The script first tests to see if the checkbox has been checked; if it has, the value of the $checkbox_form_item variable is set to 'Y'; if it hasn't, the value of the variable is set to 'N'. The field in the database table is then updated.







Go back to MySQL Tutorials home page

Go back to Tutorials home page



Earnings Tracker is John Dixon Technology's FREE open source accounting and bookkeeping software tool.

The software is written in PHP and MySQL and is available to use for FREE online, or as a FREE download.

Earnings Tracker is aimed at contractors and freelancers, and lets you record invoice amounts, salaries, income tax, pension contributions, employers and employees national insurance contributions, and calculates the amount of VAT and corporation /business tax due, and the size of dividends that can be taken by shareholders.

Earnings Tracker can also be used simply as a dividend, corporation tax, or VAT calculator.

free accounting software
 


JDT

Copyright Notice for John Dixon Technology Ltd

Privacy Statement

Terms & Conditions