How to Save Checkbox Value in Database Using Jquery

How to Save Checkbox Value in Database Using Jquery

The checkbox element in HTML allows us to select multiple items from the group of values.

When you use it in your form and try to read all checked values as any other elements like –  text box, text area, radio button, etc.

echo $_POST['lang'];  // Checkbox element

you will get the last checked value.

You need to send the checkboxes value in the form of an Array when the form gets submitted then you can loop over $_POST values.

Get checked Checkboxes value with PHP


Contents

  1. Read $_POST checked values
  2. Demo
  3. Table structure
  4. Configuration
  5. Insert and Display checked values from Database
  6. Conclusion

1. Read $_POST checked values

HTML

While creating multiple checkboxes add [] at the end of name attribute e.g. lang[]. Here, [] denotes an Array.

<span>Select languages</span><br/> <input type="checkbox" name='lang[]' value="PHP"> PHP <br/> <input type="checkbox" name='lang[]' value="JavaScript"> JavaScript <br/> <input type="checkbox" name='lang[]' value="jQuery"> jQuery <br/> <input type="checkbox" name='lang[]' value="Angular JS"> Angular JS <br/>

PHP

When the form submitted then loop over $_POST checkbox name using foreach.

if(isset($_POST['submit'])){      if(!empty($_POST['lang'])) {             foreach($_POST['lang'] as $value){             echo "value : ".$value.'<br/>';         }     }  }

Completed Code

<form method="post" action="">     <span>Select languages</span><br/>     <input type="checkbox" name='lang[]' value="PHP"> PHP <br/>     <input type="checkbox" name='lang[]' value="JavaScript"> JavaScript <br/>     <input type="checkbox" name='lang[]' value="jQuery"> jQuery <br/>     <input type="checkbox" name='lang[]' value="Angular JS"> Angular JS <br/>      <input type="submit" value="Submit" name="submit"> </form>  <?php if(isset($_POST['submit'])){      if(!empty($_POST['lang'])) {          foreach($_POST['lang'] as $value){             echo "value : ".$value.'<br/>';         }      }  } ?>

2. Demo

View Demo


3. Table structure

I am using languages table in the example.

CREATE TABLE `languages` (   `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,   `language` varchar(80) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

4. Configuration

Create a new config.php file.

Completed Code

<?php  $host = "localhost"; /* Host name */ $user = "root"; /* User */ $password = ""; /* Password */ $dbname = "tutorial"; /* Database name */  $con = mysqli_connect($host, $user, $password,$dbname); // Check connection if (!$con) {   die("Connection failed: " . mysqli_connect_error()); }

5. Insert and Display checked values from Database

Create an Array $languages_arr to store languages names.

Using this to create checkboxes by looping on it.

Insert –

On <form> submit convert$_POST['lang'] to string using implode(). Check entry in languages table if not exists then insert $lang in the table otherwise, update language value.

Display –

Fetch record from languages table. If a record exists then explode $result['language'] to get an Array and assign in $checked_arr.

While looping on $languages_arr Array check $language value exists in $checked_arr Array. If exists then assign "checked" to $checked and use in the checkbox creation.

Completed Code

<?php include "config.php"; ?> <!doctype html> <html>   <head>    <?php   if(isset($_POST['submit'])){      if(!empty($_POST['lang'])) {        $lang = implode(",",$_POST['lang']);        // Insert and Update record       $checkEntries = mysqli_query($con,"SELECT * FROM languages");       if(mysqli_num_rows($checkEntries) == 0){         mysqli_query($con,"INSERT INTO languages(language) VALUES('".$lang."')");       }else{         mysqli_query($con,"UPDATE languages SET language='".$lang."' ");       }       }    }   ?>   </head>   <body>   <form method="post" action="">     <span>Select languages</span><br/>     <?php      $checked_arr = array();      // Fetch checked values     $fetchLang = mysqli_query($con,"SELECT * FROM languages");     if(mysqli_num_rows($fetchLang) > 0){       $result = mysqli_fetch_assoc($fetchLang);       $checked_arr = explode(",",$result['language']);     }      // Create checkboxes     $languages_arr = array("PHP","JavaScript","jQuery","AngularJS");     foreach($languages_arr as $language){        $checked = "";       if(in_array($language,$checked_arr)){         $checked = "checked";       }       echo '<input type="checkbox" name="lang[]" value="'.$language.'" '.$checked.' > '.$language.' <br/>';     }     ?>       <input type="submit" value="Submit" name="submit">   </form>    </body> </html>

6. Conclusion

Next time when you use multiple checkboxes in your form then just initialize the name as an Array by putting [] in front and read it with loop when submitted.

Use implode() to convert checked values Array to string and store it to your MySQL database.

If you found this tutorial helpful then don't forget to share.

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request.

Related posts:

Source: https://makitweb.com/get-checked-checkboxes-value-with-php/

Posted by: opasboureack.blogspot.com

0 Response to "How to Save Checkbox Value in Database Using Jquery"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel