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. 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

 <html>  
      <head>  
           <title>Webslesson Tutorial</title>  
      </head>  
      <body>  
           <h3>Please Select Programming Language</h3>  
           <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" value="Submit" />  
           </form>  
      </body>  
 </html>  
 <?php  
 if(isset($_POST["submit"]))  
 {  
      if(!empty($_POST["language"]))  
      {  
           echo '<h3>You have select following language</h3>';  
           foreach($_POST["language"] as $language)  
           {  
                echo '<p>'.$language.'</p>';  
           }  
      }  
      else  
      {  
           echo "Please Select Atleast one Programming language";  
      }  
 }  
 ?>  

0 comments:

Post a Comment