我试图根据POST数据从db获取数据,但是我得到了一些奇怪的错误:
Uncaught SyntaxError: Unexpected token < in JSON at position 0
我很确定我的数据查询有问题 - 错误的语法/错误的类型,但我不能var_dump变量值,所以我有点无能为力。
<?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->getQuery(true);
$query->select('*');
$query->from($db->quoteName('#__products'));
$query->where($db->quoteName('id') . ' = (', implode(",",$data));
// var_dump($query->__toString()); Doesnt work... Why?
$db->setQuery($query);
$results = $db->loadObjectList();
$response = $data;
echo new JResponseJson($response);
jexit();
}
?>
<form method="post" action="" name="form1" id="mycustomform">
<input type="checkbox" name="checkboxList[]" onclick="function1();" value="1"> Apple</input><br>
<input type="checkbox" name="checkboxList[]" onclick="function1();" value="2"> Pineapple</input><br>
<input type="checkbox" name="checkboxList[]" onclick="function1();" value="3"> Pen</input>
</form>
<div id="response"></div>
<script>
function function1() {
var data = jQuery("#mycustomform").serialize() + '&thisisajax=1';
console.log(data);
jQuery.ajax({
url: "<?php echo JUri::current(); ?>",
type: "POST",
data: data,
success: function(response){
response = jQuery.parseJSON(response);
if(response.success === true){
jQuery('#response').html(response.data);
} else {
alert('error');
}
}
});
}
</script>