Getting checkbox values on submit in PHP with example

Getting checkbox values on submit in PHP with example

If you have multiple options, then you want to get the checked values to store them in variable on form submit, Then you can use this technique.

A good method which is a favorite of mine and for many developers, is to make use of foreach which will output each value you chose, and appear on screen one underneath each other.

Here is an example using $_POST. You can however use $_GET to work properly.

<form action="#" method="POST">
<input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
<input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
<input type="checkbox" name="check_list[]" value="jQuery"><label>jQuery</label><br/>
<input type="checkbox" name="check_list[]" value="Android"><label>Android</label><br/>
<input type="checkbox" name="check_list[]" value="SQL"><label>SQL</label><br/>
<input type="checkbox" name="check_list[]" value="Laravel"><label>Laravel</label><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
if(isset($_POST['submit'])){//to run PHP script on submit form
 if(!empty($_POST['check_list'])){
  //Loop to store and display values of individual checked checkbox.
  foreach($_POST['check_list'] as $selected){
   echo $selected."</br>";
  }
 }
}

In our example, there is a form contains some checkboxes, User checks them and when user hits submit button, multiple values of checkboxes will be display.

Related posts

Write a comment