title: Fetch data from DB based on POST data using AJAX
body: <p>I'm trying to fetch data from db based on POST data, but I get some weird error:</p>
  
  <pre><code>Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0
  </code></pre>
  
  <p>Im pretty sure something is wrong with my data query  - wrong syntax / wrong types, but I can not var_dump variable values, so I'm kinda clueless.</p>
  
  <pre><code>&lt;?php 
  defined('_JEXEC') or die;
  
  $data = [];
  $ajax_test = [];
  
  if(isset($_POST['checkboxList'])){
  $data = $_POST['checkboxList']; // Possible results: 1; 12; 123 (checkbox value)
  }
  if(isset($_POST['thisisajax'])){
  $ajax_test = $_POST['thisisajax'];
  }
  
  if($ajax_test){
   $db = JFactory::getDbo();
   $query = $db-&gt;getQuery(true);
   $query-&gt;select('*');
   $query-&gt;from($db-&gt;quoteName('#__products'));
   $query-&gt;where($db-&gt;quoteName('id') . ' = (', implode(",",$data));
   // var_dump($query-&gt;__toString()); Doesnt work... Why?
   $db-&gt;setQuery($query);
   $results = $db-&gt;loadObjectList();
   $response = $data;
  
  echo new JResponseJson($response);
  jexit();
  }
  
  ?&gt;
  
  &lt;form method="post" action="" name="form1" id="mycustomform"&gt;
  &lt;input type="checkbox" name="checkboxList[]" onclick="function1();" value="1"&gt; Apple&lt;/input&gt;&lt;br&gt;
  &lt;input type="checkbox" name="checkboxList[]" onclick="function1();" value="2"&gt; Pineapple&lt;/input&gt;&lt;br&gt;
  &lt;input type="checkbox" name="checkboxList[]" onclick="function1();" value="3"&gt; Pen&lt;/input&gt;
  &lt;/form&gt;
  &lt;div id="response"&gt;&lt;/div&gt;
  
  &lt;script&gt;
  function function1() {
  var data = jQuery("#mycustomform").serialize() + '&amp;thisisajax=1';
  console.log(data);
  jQuery.ajax({
  url: "&lt;?php echo JUri::current(); ?&gt;", 
  type: "POST",
  data: data, 
  success: function(response){ 
  response = jQuery.parseJSON(response);
  if(response.success === true){
  jQuery('#response').html(response.data);
  } else {
  alert('error');
  }
  }
  });
  }
  &lt;/script&gt;
  </code></pre>