Thursday 10 March 2016

How to get Multiple Checkbox values in php

In this tutorial we are going to learn how to get Multiple Checkbox values in php and Insert or Add multiple checked check boxes values into Mysql Database Table by using PHP Script. This is very simple things every experienced programmer know this things but If new programmer don't know this things. This tutorial is for beginner php programmer. In php , If we want get the single value of checkbox then it simply get by post method but problem is there if we want get the values from multiple checkbox then at that time problem occur. This things is done by define multiple checkbox with same name with array and then after use foreach to fetch all values of checkbox.

Source Code


$connect = mysqli_connect("localhost", "root", "", "testing");

           <title>Webslesson Tutorial</title> 
           <script src=""></script>
     <link rel="stylesheet" href="" />
     <script src=""></script>
    border:1px solid #ccc;
        <div class="container box">
          <h3 align="center">Get Multiple Checkbox values and Insert into Mysql in PHP</h3><br />
          <h4>Please Select Programming Language</h4><br />  
          <form method="post">
           <p><input type="checkbox" name="language[]" value="C" /> C</p>
           <p><input type="checkbox" name="language[]" value="C++" /> C++</p>
           <p><input type="checkbox" name="language[]" value="C#" /> C#</p>
           <p><input type="checkbox" name="language[]" value="Java" /> Java</p>
           <p><input type="checkbox" name="language[]" value="PHP" /> PHP</p>
           <p><input type="submit" name="submit" class="btn btn-info" value="Submit" /></p>
           $for_query = '';
            foreach($_POST["language"] as $language)
             $for_query .= $language . ', ';
            $for_query = substr($for_query, 0, -2);
            $query = "INSERT INTO tbl_language (name) VALUES ('$for_query')";
            if(mysqli_query($connect, $query))
             echo '<h3>You have select following language</h3>';
                echo '<label class="text-success">' . $for_query . '</label>';
            echo "<label class='text-danger'>* Please Select Atleast one Programming language</label>";
     <br />


  1. Please help me implement this: the checkbox are inside the datatables.
    i would like to insert into the database table the rows i selected using the checkbox. each row in the datatable has its own checkbox and i would like to insert into the database the rows i selected thru their checkbox

  2. if checkbox come from database the how can i get checked value

  3. This Script is very Helpfull

    CREATE TABLE `tbl_language` (
    `id` int(11) NOT NULL,
    `name` varchar(300) NOT NULL,
    ADD PRIMARY KEY (`id`);

    this script save your choice in the cell name
    for example:
    ID | Name
    1 | C, C++