Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
PHP ›
Checkbox in PHP

Checkbox in PHP

February 19, 2011 PHP Jesin A 4 Comments

php category thumbnail

This tutorial explains how to use checkboxes in PHP to get values which are checked by the user. Use checkboxes in PHP and insert the values into MySQL. Usually using other HTML form controls with PHP is easy. The input element is assigned a name and values entered can be retrieved using GET or POST method. But in the case of checkboxes it might be ticked or left empty also. Using checkboxes in PHP involves setting the name attribute of all checkboxes to a common name with an empty square bracket denoting an array. When the form is submitted the vales of the items checked are send as an array which is explained here

First create checkboxes inside the HTML form and name them accordingly. A sample is shown below

<input type="checkbox" name="cbox[]" value="Item 1" />Item 1
<input type="checkbox" name="cbox[]" value="Item 2" />Item 2
<input type="checkbox" name="cbox[]" value="Item 3" />Item 3
<input type="checkbox" name="cbox[]" value="Item 4" />Item 4
<input type="checkbox" name="cbox[]" value="Item 5" />Item 5

Now getting the checkbox value in PHP is very easy. The values of the checked checkboxes are stored in the array cbox. So in the PHP you’ll retrieve them as $_POST[‘cbox’][index] or $_GET[‘cbox’][index] or $_REQUEST[‘cbox’][index] we’ll use the implode() function to glue together all checked values to display them

<?php
$items = implode(",",$_REQUEST['cbox']);
print "You selected $items";
?>

The output of the above code will be “You selected item 2,Item 4,Item 5” provided the user checked the Items 2, 4 and 5. You can also insert the values into the MySQL database using PHP in the same way as a complete string with values separated by commas. For example you want to enter the hobbies of a student into the MySQL database.

<?php
$hobbies = implode(",",$_REQUEST['hobbies']);
mysql_query("INSERT INTO `table_name` (hobbies) VALUES ('$hobbies')");
?>

To retrieve them from the database and display as a list use the explode() function to separate the values and display them through foreach statement.

<?php
$hobbies = mysql_fetch_array(mysql_query("SELECT `hobbies` FROM `table_name` WHERE name='jesin'"));
$hobby = explode(",",$hobbies);
print "<ul>";
foreach ($hobby as $item)
{
print "<li>$item</li>";
}
print "</ul>";
?>

See a live demo of using checkboxes in PHP. Select the films of your choice and you’ll see them as a list.

Related posts:

web design category thumbnailJavascript Validation – Textbox Combobox Radiobutton Checkbox php category thumbnailMultiple submit buttons in PHP php category thumbnailPHP Pie Chart Script connect php with mysql thumbnailConnect PHP with MySQL web design category thumbnailCSS Horizontal Navigation Menu

Tags: php, php in html

Comments

  1. Ozi says

    June 16, 2014 at 7:38 am

    Thanks for share Jesin, good job.

    Reply
  2. tel says

    July 5, 2014 at 12:17 am

    can you give an example how to email item 2,3,5….. ONLY

    Reply
  3. Onyema says

    April 9, 2018 at 4:09 am

    Easy lesson bro. Thanks for saving my day!

    Reply
  4. Sahar says

    December 7, 2020 at 6:23 pm

    Thank you so much for your help.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.