I have a form with integrated the plugin querybuilder.js.org
My js is:
FROM: scripts.js
$(document).ready(function(e) {
 // INIT PLUGIN
 $('#queryBuilder').queryBuilder({
   plugins: ['bt-tooltip-errors'],
   filters: [{
     id: 'description',
     label: 'Description',
     type: 'string'
   }]
 });
 // RESTORE RULES FROM LAST SEARCH
 $('#queryBuilder').queryBuilder('setRules', decodeURIComponent($("#rules").val()) );
 // ON SUBMIT, EDIT THE FORM AND APPEND SQL, PARAMS to USE IN SEARCH FROM DATABASE, AND APPEND RULES TO RESTORE IT TO NEXT LOAD OF THE PAGE
 $("#btn-submit").on("click", function(e) {
    e.preventDefault(); 
    var form = $("#form");  
    var rules = $('#queryBuilder').queryBuilder('getRules');
    var query = $('#queryBuilder').queryBuilder('getSQL', 'question_mark');
    // FOR RESTORE INITIAL VALUES ON RELOAD PAGE            
    form.append('<input type="hidden" name="rules" value="' + encodeURIComponent(JSON.stringify(rules, null, 2)) + '">');
    form.append('<input type="hidden" name="sql" value="' + query.sql + '">');
    form.append('<input type="hidden" name="params" value="' + encodeURIComponent(JSON.stringify(query.params, null, 2)) + '">');
    form.submit();
 });
});
My piece of code in index.php is:
<?php
  if ( isset($_POST) ) {
  $rules = isset($_POST['rules']) ? $_POST['rules'] : "";
  }
  ?>
  <form name="form" id="form" role="form" method="post" action="">
    <div id="queryBuilder"></div>
    <!--used only to restore rules in jquery-querybuilder-->
    <input type="hidden" id="rules" value="<?php echo $rules; ?>">
    <button type="button" id="btn-submit" class="btn btn-success">Search</button>
  </form>
Problem is that rules are not restored as expected.
If I insert an alert on document ready like:
alert(decodeURIComponent($("#rules").val()));
I get correct rules:
{
  "condition": "AND",
  "rules": [
    {
      "id": "description",
      "field": "description",
      "type": "string",
      "input": "text",
      "operator": "equal",
      "value": "test value"
    }
  ],
  "valid": true
}
It seem this $('#queryBuilder').queryBuilder('setRules', decodeURIComponent($("#rules").val()) ); is not a valid way to init the plugin.
If I try to hard-code the rules in this way:
var rules_basic = {
  "condition": "AND",
  "rules": [
    {
      "id": "description",
      "field": "description",
      "type": "string",
      "input": "text",
      "operator": "equal",
      "value": "test value"
    }
  ],
  "valid": true
};
$('#queryBuilder').queryBuilder({
   plugins: ['bt-tooltip-errors'],
   filters: [{
     id: 'description',
     label: 'Description',
     type: 'string'
   }],
   rules: rules_basic 
 });
Then all work correctly!
How can I force to get init rules from an dynamic hidden input field? My script.js is separate from HTML so i can't (and I don't want) to force writing initial rules using PHP like:
index.php:
.... HTML PART
..... JS PART
$('#queryBuilder').queryBuilder({
   plugins: ['bt-tooltip-errors'],
   filters: [{
     id: 'description',
     label: 'Description',
     type: 'string'
   }],
   rules: <?php echo $rules; ?>    // <---------------- HERE
 });
 
     
    