title: How to save changes back to databse from jsTree?
body: <p>I want to save the changes i make on the jsTree back to database. I tried different methods but cant get to work. I am working in codeigniter. Please help me if you can. Thank you.
  Here is my code,
  view.php:</p>
  
  <pre><code> &lt;button id="target" type="submit"&gt;Click here&lt;/button&gt;
  
  &lt;script type="text/javascript"&gt;
      $(document).ready(function () {
          $('#tree-container').on('changed.jstree', function (e, data) {
              var i, j, r = [];
              // var state = false; 
              for (i = 0, j = data.selected.length; i &lt; j; i++) {
                  r.push(data.instance.get_node(data.selected[i]).id);
              }
              $('#txttuser').val(r.join(','));
          }).jstree({
              'plugins': ["checkbox", "dnd", "search", "contextmenu"],
              'core': {
                  "check_callback" : true,
                  "multiple": true,
                  'data': {
                      "url": "getChildren",
                      "dataType": "json"
                  }
              },
              'checkbox': {
                  three_state: false,
                  cascade: 'up'
              },
              'plugins': ["dnd","contextmenu","search","wholerow"],
          }
          );
      });
  
     $( "#target" ).click(function() { 
         var v =$('#tree-container').jstree(true).get_json();      
         var mytext = JSON.stringify(v);      
         alert(mytext); });
  &lt;/script&gt;
  
  &lt;div class="row"&gt; 
      &lt;div class="container"&gt; 
          &lt;input type="hidden" name="node" id="node" value="" /&gt; 
          &lt;div class="form-group"&gt; &lt;div id="tree-container"&gt;&lt;/div&gt; 
          &lt;/div&gt; 
      &lt;/div&gt; 
  &lt;/div&gt;
  </code></pre>
  
  <p>controller.php:</p>
  
  <pre><code> function getChildren() { 
  
      $this-&gt;load-&gt;model('Category_model');
  
      $result = $this-&gt;Category_model-&gt;tree_all(); 
  
      $itemsByReference = array(); 
   // Build array of item references: 
          foreach($result as $key =&gt; &amp;$item) { 
          $itemsByReference[$item['category_id']] = &amp;$item; 
   // Children array:
          $itemsByReference[$item['category_id']]['children'] = array(); 
   // Empty data class (so that json_encode adds "data: {}" ) 
          $itemsByReference[$item['category_id']]['data'] = new StdClass(); 
  
      } 
   // Set items as children of the relevant parent item. 
      foreach($result as $key =&gt; &amp;$item) 
          if($item['parent_id'] &amp;&amp;       isset($itemsByReference[$item['parent_id']])) 
              $itemsByReference [$item['parent_id']]['children'][] = &amp;$item; 
  
  
   // Remove items that were added to parents elsewhere: 
      foreach($result as $key =&gt; &amp;$item) { 
          if($item['parent_id'] &amp;&amp;    isset($itemsByReference[$item['parent_id']])) 
              unset($result[$key]); 
  
  
  
      } 
  
      foreach ($result as $row) { 
          $data[] = $row; 
  
      } 
   // Encode: 
  echo json_encode($data); 
  
      }
  </code></pre>
  
  <p>model.php:</p>
  
  <pre><code> function tree_all() { 
  $result = $this-&gt;db-&gt;query("SELECT category_id, category,category as text,   parent_id FROM category ")-&gt;result_array(); 
  foreach ($result as $row) { 
      $data[] = $row; 
  
  } 
      return $data; 
  
  }
  </code></pre>
  
  <p>Pleeaaase heeelp meee!!! I am trying to figure this out for days.</p>