到目前为止,我还没有看到如何使用我搜索的解决方案" ajax php create associative array然后返回sql results" - 需要帮助:我的提交按钮调用jQuery.ajax,将选择数据发布到PHP网址;然后我的retrievever.php文件[msSQL]查询一个表并返回多个记录。我想然后将数组返回到我的jQuery.ajax成功函数,并解析返回的数据 - 并填充DataTables HTML表的行。这是来自caller.php的jQuery.ajax:
jQuery('#submit').click(function() {
jQuery.ajax({
url: "https://domain/projects/current/retrieve.php",
//contentType: "application/json",
data: {fy: jQuery('#fy_select').val(), lpid: jQuery('#lp_select').val(), next_year: jQuery('#fy_select').val()+1 },
//dataType: "json",
type: 'post',
success: function(data, XMLHttpRequest){
//alert(data.length);
//alert(JSON.parse(data));
//alert(data[0]);
//jQuery('#activity').text("[" + JSON.parse(data)[0] + "] " + JSON.parse(data)[1]);
jQuery.each(data, function () {
jQuery('#activity').text("[" + JSON.parse(data)[0] + "] " + JSON.parse(data)[1]);
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
return false;
});
请原谅我在我的代码中留下的评论,因为如果你看到我试过的可以打捞的东西让我知道;否则我希望他们不会分散注意力。我只需要查询数据库并获取结果,将它们填入数组并将其返回到ajax,然后解析活动,从sql结果中命名。现在是retrieve.php代码。提前一如既往!:
<?php
include(MSS_DB);
//get vars from jQuery .ajax post - lpid and fy:
if ($_POST['lpid'] != null && $_POST['fy'] != null ){
$lpid = $_POST['lpid'];
$fy = $_POST['fy'];
$next_year = $_POST['next_year'];
$fund_info_get = "select activityid, name from activity_table where office= " . $office . " and approveddate > '" . $fy . "-06-30' order by activityid desc";
$get_fund_result = mssql_query($fund_info_get);
$data_array = array();
//$data_array = mssql_fetch_array($get_fund_result);
while($row = mysql_fetch_assoc($get_fund_result)){
$data_array = array('activity_id' => '$row['activityid']', 'activity_name' => '$row['name']');
//$data_array [] = $row; //tried this from another post.
}
$rowcount=mssql_num_rows($get_fund_result); //for alexander this returns 11, correct.
/*while($row = mysql_fetch_array($get_fund_result)){
$table_data[]= arrsay("id=>" = '"$row['activityid']"', "name=>" = '"$row['name']"');
}*/
//$result = "lpid/fy/next_year/get_fund_result: " . $lpid . "/" . $fy . "/" . $next_year . "/" . $fund_info_get . "";
}else{
// either of the values doesn't exist
$result = "No Data Was Sent !";
}
//echo $rowcount;
//echo json_encode($table_data);
//echo $data_array;
echo json_encode($data_array);
//echo $result;
答案 0 :(得分:0)
thx,全部供您参考!它帮助我前进了。对我的解决方案的简要描述可能会帮助您了解每个人的帮助:
目标:jQuery.ajax调用someurl.php,将mssql查询数据转换为数组,返回json_encode数组。回到jQuery.ajax成功,通过json数据解析。以下是每个文件的操作功能以显示解决方案:
callerfile.php:
jQuery('#submit').click(function() {
jQuery.ajax({
url: "https://fabrik.smartstartinc.net/ncpcphp/activity-mgmt2.0/porc_get_activities.php",
data: {fy: jQuery('#fy_select').val(), lpid: jQuery('#lp_select').val(), next_year: jQuery('#fy_select').val()+1 },
dataType: "json",
type: 'post',
success: function(data, XMLHttpRequest){
console.log(data); //shows json: [{"activity_id":11111,"activity_name":"Community Cleanup"}, etc
jQuery.each(data, function(index, value){
console.log(data[index].activity_id);
console.log(data[index].activity_name);
//these show the id, the name.
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
return false;
});
返回json数组的PHP文件,retrieve.php:
$fund_info_get = "select activityid, name from cpy_activity where lpid= " . $lpid . " and approveddate > '" . $fy . "-06-30' order by activityid desc";
$get_fund_result = mssql_query($fund_info_get);
$data_array = array();
while($row = mssql_fetch_assoc($get_fund_result)){
$data_array[] = array('activity_id' => $row['activityid'], 'activity_name' => $row['name']);
}
}else{
// either of the values doesn't exist
$result = "No Data Was Sent !";
}
echo json_encode($data_array);
一旦我使用了JSON.stringify并看到了数据的格式,我就可以开始深入了解如何显示它。再次!!