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


<?php

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

?>
<html>  
      <head>  
           <title>Webslesson Tutorial</title> 
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
     <style>
   
   .box
   {
    width:750px;
    padding:20px;
    background-color:#fff;
    border:1px solid #ccc;
    border-radius:5px;
    margin-top:100px;
   }
  </style>
      </head>  
      <body>  
        <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>
          </form>
          <?php
          if(isset($_POST["submit"]))
          {
           $for_query = '';
           if(!empty($_POST["language"]))
           {
            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>';
            }
           }
           else
           {
            echo "<label class='text-danger'>* Please Select Atleast one Programming language</label>";
           }
          }
          ?>
     <br />
         </div>  
      </body>  
 </html>

3 comments:

  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

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

    ReplyDelete
  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++

    ReplyDelete