Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert jstree checkbox value into database

<script type="text/javascript">
$(document).ready(function(){ 
    //fill data to tree  with AJAX call
    $('#tree-container').jstree({
	'plugins': ["wholerow", "checkbox"],
        'core' : {
            'data' : {
                "url" : "response.php",
                "dataType" : "json" // needed only if you do not supply JSON headers
            }
        }
    })
});
</script>


 <div id="tree-container"></div>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "defectsystem";

$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$sql = "SELECT * FROM `treeview_items` ";
$res = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
	//iterate on results row and create new index array of data
	while( $row = mysqli_fetch_assoc($res) ) { 
		$data[] = $row;
	}
	$itemsByReference = array();

// Build array of item references:
foreach($data as $key => &$item) {
   $itemsByReference[$item['id']] = &$item;
   // Children array:
   $itemsByReference[$item['id']]['children'] = array();
   // Empty data class (so that json_encode adds "data: {}" ) 
   $itemsByReference[$item['id']]['data'] = new StdClass();
}

// Set items as children of the relevant parent item.
foreach($data as $key => &$item)
   if($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
      $itemsByReference [$item['parent_id']]['children'][] = &$item;

// Remove items that were added to parents elsewhere:
foreach($data as $key => &$item) {
   if($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
      unset($data[$key]);
}
// Encode:
echo json_encode($data);
?>

I had successfully create a jstree with checkbox. However, how I can insert the checkbox value into the database when I click it and submit.

Thankss if anyone could help me!! If any question can ask me below comment.

like image 571
eric Avatar asked Jan 25 '26 10:01

eric


1 Answers

Try some thing like this:

var array = [];
// create an array

$.each($("input[name='user_permission']:checked"), function(){
    permissions.push($(this).val());
});
// Iterate over each checkbox which is checked and push its value in the array variable.

Ex:

......
var permissions = [];
$.each($("input[name='user_permission']:checked"), function(){
    permissions.push($(this).val());
});

$.ajax({
    url         : 'add_permission.php',
    method      : 'post',
    data       :
    {
        permissions : JSON.stringify(permissions)
    }
    ....
});
// After complete iteration you will get the value of each checked checkbox. 

Now insert it in database using ajax call

like image 183
Mayank Pandeyz Avatar answered Jan 27 '26 22:01

Mayank Pandeyz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!