1

我在从 mysql 表中检索数据进行 ajax 调用时遇到问题。我正在使用 jquery 数据表来显示数据。代码如下

var oTable;

/* Formating function for row details */
function fnFormatDetails ( nTr )
{
var aData = oTable.fnGetData( nTr );
var sOut = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">';
sOut += '<tr><td>'+aData[0]+'</td></tr>';
sOut += '</table>';

return sOut;
}

$(document).ready(function() {
oTable = $('#datatables').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "../getproducts.php",
    "aData" : "POST","getproducts.php?c_name_mfac="+c_name_mfac
    "aoColumns": [
        { "sClass": "center", "bSortable": false },
        null,
        { "sClass": "center" },
    ]
} );

$('#datatables tbody tr td img').live( 'click', function () {
    var nTr = this.parentNode.parentNode;
    if ( this.src.match('details_close') )
    {
        /* This row is already open - close it */
        this.src = "../images/details_open.png";
        oTable.fnClose( nTr );
    }
    else
    {
        /* Open this row */
        this.src = "../images/details_close.png";
        oTable.fnOpen( nTr, fnFormatDetails(nTr), 'details' );
    }
} );
} );

代码的HTML部分如下:

<div>                          
<table id = "datatables" class="display">
<thead>
<tr>
    <th></th>
    <th>Company Name</th>
</tr>
<thead>
<tbody>
    <?while($row=mysql_fetch_array($result)){               
            <tr>
                <td><img src="images/details_open.png"/></td>
        <td class="center" value="c_name_mfac"><?= $row['c_name_mfac']?></td>
    </tr>
    <?}}?>
</tbody>
</table>
</div>  

对文件 getproducts.php 进行 AJAX 调用,代码如下所示:

<?php
   include('config.php');
   $cname = $_POST['c_name_mfac'];

   $sql = mysql_query("SELECT * FROM items where c_name_mfac = $cname ");

?>

我没有收到任何语法错误,但也没有得到输出。

我是 AJAX 和 JQUERY 的初学者。基本上我需要的是基于我需要在 jquery 数据表中显示其所有产品的公司名称。我正在使用DataTables 隐藏行详细信息示例,但这似乎对我有用.. 任何人都可以帮忙吗?

谢谢,数控

4

1 回答 1

0

PHP 文件没有返回任何值,您应该生成一些 JSON 输出,如下所示:

<?php
   include('config.php');
   $cname = $_POST['c_name_mfac'];

   $sql = mysql_query("SELECT * FROM items where c_name_mfac = $cname ");}

   $result = new stdObject;
   $result->aaData = array();

   while($row = mysql_fetch_assoc($sql)) {
       array_push($result->aaData, $row);
   }

   header('Cache-Control: no-cache, must-revalidate');
   header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
   header('Content-type: application/json');

   echo json_encode($result);
?>
于 2012-10-16T23:36:03.567 回答