我正在开发一个CodeIgniter项目,而且我没有从AJAX帖子中获取任何数据。我有一个表单设置来运行各种参数的搜索。 PHP肯定是获取正确的数据 - 我有错误日志确认PHP本身正在运行 - 但jQuery方面没有任何结果。我试过回声数组,json_encoding数组,只是返回数组等,但没有任何结果。 “成功”和“错误”功能根本不执行。我发布的是文件的最新版本;我尝试了变化......但首先,在模型中调用的方法是:
function search_games($data) {
$dbSelect = "SELECT `gameTitle`";
$include_count = 0;
// Count items in arrays - console, stuff to include, stuff to exclude
foreach ($data['include'] as $include => $val) {
if ($val === 'true') {
$include_count++;
$dbSelect .= ',`'.$include.'`';
}
}
$dbSelect .= " FROM tbUserGames ";
$console_count = count($data['console']);
// Include these consoles
switch ($console_count) {
case 1:
$dbWhere = "WHERE ((gamePlatform=".$data['console'][0]." ";
break;
case 2:
$dbWhere = "WHERE ((gamePlatform=".$data['console'][0];
$dbWhere .= " OR gamePlatform=".$data['console'][1]." ";
break;
case 3:
$dbWhere = "WHERE ((gamePlatform=".$data['console'][0];
$dbWhere .= " OR gamePlatform=".$data['console'][1];
$dbWhere .= " OR gamePlatform=".$data['console'][2]." ";
break;
}
$dbWhere .= ")";
// Include games that have manual, box, and/or notes:
if ($data['manual'] == 'true') {
$dbWhere .= " AND gameManual=1";
}
if ($data['box'] == 'true') {
$dbWhere .= " AND gameBox=1";
}
if ($data['notes'] == 'true') {
$dbWhere .= " AND notes IS NOT NULL";
}
// Exclude games that have manual, box, and/or notes
if ($data['emanual'] == 'true') {
$dbWhere .= " AND NOT gameManual=1";
}
if ($data['ebox'] == 'true') {
$dbWhere .= " AND NOT gameBox=1";
}
if ($data['enotes'] == 'true') {
$dbWhere .= " AND notes IS NULL";
}
// game titles start with, contain, or end with...
switch($data['search_terms']) {
case 'start_with':
$dbLike = " AND gameTitle LIKE '".$data['search_key']."%'";
break;
case 'contain':
$dbLike = " AND gameTitle LIKE '%".$data['search_key']."%'";
break;
case 'end_with':
$dbLike = " AND gameTitle LIKE '%".$data['search_key']."'";
break;
}
// AA Rarity rating: exactly, at least, at most, greater than, less than, all but
$dbWhere .= " AND ((atariagecom_rarity ";
switch($data['rarity_fuzz_drop']) {
// NOTE: THESE CASES NEED FIXING TO WORK WITH HOMEBREWS, PROTOS, REPROS, AND UNKNOWNS
case 'at_least':
$dbWhere .= "BETWEEN ".$data['rarity_options']." AND 10)";
break;
case 'greater_than':
$dbWhere .= "BETWEEN ".((int)$data['rarity_options']+1)." and 10)";
break;
case 'exactly':
$dbWhere .= "= ".$data['rarity_options'].")";
break;
case 'less_than':
$dbWhere .= "< ".$data['rarity_options'].")";
break;
case 'at_most':
$dbWhere .= "<= ".$data['rarity_options'].")";
break;
case 'all_but':
$dbWhere .= "!= ".$data['rarity_options']." AND atariagecom_rarity < 11)";
}
/*
* Binary format for non-numeric rarity:
* homebrew: 1 atariage_com rarity: 11
* repro: 2 atariage_com rarity: 12
* proto: 4 atariage_com rarity: 13
* unknown: 8 atariage_com rarity: 14
*/
$rar_bin = 0;
// Include homebrews..
if ($data['aah'] == 'true') {
$rar_bin += 1;
}
// Include repros...
if ($data['aar'] == 'true') {
$rar_bin += 2;
}
// Include protos...
if ($data['aap'] == 'true') {
$rar_bin += 4;
}
// Include games of undetermined rarity...
if ($data['aau'] == 'true') {
$rar_bin += 8;
}
if ($rar_bin > 0) {
$dbWhere .= " OR ";
// account for rarity of 11
if ($rar_bin % 2 === 1) {
$dbWhere .= "(atariagecom_rarity=11)";
if ($rar_bin > 1) {
$dbWhere .= " OR " ;
}
}
// account for rarity of 12
if ($rar_bin == 2 || $rar_bin == 3) {
$dbWhere .= "(atariagecom_rarity=12)";
}
// rarity 13 only
if ($rar_bin == 4) {
$dbWhere .= "(atariagecom_rarity=13)";
}
// account for rarity of both 12 and 13
if ($rar_bin==5 || $rar_bin==6 || $rar_bin==7 || $rar_bin==12 || $rar_bin==13 || $rar_bin==14 || $rar_bin==15) {
$dbWhere .= "(atariagecom_rarity=12) OR (atariagecom_rarity=13)";
}
if ($rar_bin > 8) {
$dbWhere .= " OR ";
}
// rarity 14
if ($rar_bin >= 8) {
$dbWhere .= "(atariagecom_rarity=14)";
}
$dbWhere .=")";
}
$dbLike .= ")";
$dbOrderBy = " ORDER BY gameTitle";
$sql = $dbSelect.$dbWhere.$dbLike.$dbOrderBy;
$query = $this->db->query($sql);
return $query->result_array();
}
该模型肯定会提取正确的数据,因此可行。
这是在控制器中执行的方法 - 这也适用:
function do_search() {
$this->load->model('listgames_model');
if ($_POST['a2600'] === 'true') {
$search_games['console'][] = '2';
}
if ($_POST['a5200'] === 'true') {
$search_games['console'][] = '5';
}
if ($_POST['a7800'] === 'true') {
$search_games['console'][] = '7';
}
$search_games['manual'] = $_POST['manual'];
$search_games['box'] = $_POST['box'];
$search_games['notes'] = $_POST['notes'];
$search_games['emanual'] = $_POST['manual'];
$search_games['ebox'] = $_POST['box'];
$search_games['enotes'] = $_POST['notes'];
$search_games['include']['smanual'] = $_POST['manual'];
$search_games['include']['sbox'] = $_POST['box'];
$search_games['include']['snotes'] = $_POST['notes'];
$search_games['search_terms'] = $_POST['search_terms'];
$search_games['search_key'] = $_POST['search_key'];
$search_games['rarity_fuzz_drop'] = $_POST['rarity_fuzz_drop'];
$search_games['rarity_options'] = $_POST['rarity_options'];
$search_games['aah'] = $_POST['aah'];
$search_games['aar'] = $_POST['aar'];
$search_games['aap'] = $_POST['aap'];
$search_games['aau'] = $_POST['aau'];
$search_games['include']['gamePlatform'] = $_POST['splatform'];
$search_games['include']['atariagecom_rarity'] = $_POST['rarity'];
$search_games['include']['cartColor'] = $_POST['cart_color'];
$search_games['include']['labelCase'] = $_POST['label_case'];
$search_games['include']['labelStyle'] = $_POST['label_style'];
$search_games['include']['overlay'] = $_POST['overlay'];
$search_result = $this->listgames_model->search_games($search_games);
if (is_array($search_result)) {
error_log("IT'S AN ARRAY");
$this->load->library('session');
$this->session->set_userdata('game_data',$search_result);
error_log(print_r($search_result,1));
error_log("echoing");
echo json_encode($search_result);
} else {
return array('success' => false);
}
}
这是AJAX电话:
function validate_list_form() {
var errors = new Array();
var form_data = new Array();
if (($('#a2600').prop('checked') !== true) && ($('#a5200').prop('checked') !== true) && ($('#a7800').prop('checked') !== true)) {
alert("You need to choose at least one console.");
return false;
} else {
$.ajax({type: 'post',
url: 'listgames/do_search',
data: {
'a2600': $('#a2600').prop('checked'),
'a5200': $('#a5200').prop('checked'),
'a7800': $('#a7800').prop('checked'),
'manual': $('#manual').prop('checked'),
'box': $('#box').prop('checked'),
'notes': $('#notes').prop('checked'),
'emanual': $('#emanual').prop('checked'),
'ebox': $('#ebox').prop('checked'),
'enotes': $('#enotes').prop('checked'),
'search_terms': $('#search_terms').val(),
'search_key' : $('#search_key').val(),
'rarity_fuzz_drop' : $('#rarity_fuzz_drop').val(),
'rarity_options' : $('#rarity_options').val(),
'aah': $('#aah').prop('checked'),
'aar': $('#aar').prop('checked'),
'aap': $('#aap').prop('checked'),
'aau': $('#aau').prop('checked'),
'splatform': $('#splatform').prop('checked'),
'smanual': $('#smanual').prop('checked'),
'sbox': $('#sbox').prop('checked'),
'snotes': $('#snotes').prop('checked'),
'rarity': $('#rarity').prop('checked'),
'cart_color': $('#cart_color').prop('checked'),
'label_style': $('#label_style').prop('checked'),
'label_case': $('#label_case').prop('checked'),
'overlay': $('#overlay').prop('checked')
},
dataType: 'json',
success: function(result) {
console.log("Here's the result:");
console.dir(result);
},
error: function(result) {
console.log("ERROR:");
console.dir(result);
},
always: function(result) {
console.log("THIS WILL RUN NO MATTER WHAT");
console.dir(result);
}
});
}
}
Firebug总是告诉我“这将无关紧要”,但我什么都没得到。再一次,PHP给了我一些我想要的东西。它只是不会将数据返回给jQuery。
我做得不对?
答案 0 :(得分:0)
在你的模型中,你需要回显json而不是返回php数组。
} else {
return array('success' => false);
}
应该是
} else {
echo json_encode(array('success' => false));
}
此外,您使用'sbox': $('#sbox').prop('checked'),
的名称发送并使用错误的名称收到$_POST['box']
检查此行
$search_games['include']['sbox'] = $_POST['box'];
其他一些行也有同样的错误。
答案 1 :(得分:0)
我想通了......从我遇到这个问题起,它花了大约一个月的时间。我的jQuery,我的数据或我的PHP没有任何问题。它是......我使用CodeIgniter的方式。
我使用CodeIgniter的表单助手来使用提交按钮 - 问题出在哪里。使用提交按钮使CodeIgniter实际执行其标准表单提交功能,这会导致重新加载或重定向。由于该重定向,AJAX调用在完成之前被取消,因此没有给我任何结果。我将提交按钮更改为常规按钮,热潮!得到了我需要的结果!