douken1726 2012-08-25 05:39
浏览 42

通过使用jquery数据表进行ajax调用从mysql表中检索数据

I am having a problem while retrieving data from mysql table making an ajax call. I am using jquery datatables to display the data. The code is as follows

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' );
    }
} );
} );

The HTML part of the code is as follows:

<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>  

The AJAX call is made to the file getproducts.php The code is as shown below :

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

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

?>

I am not getting any syntax error but not getting the output also.

I am a beginner in AJAX and JQUERY. Basically what I need is based on a company name I need to display all its products in the jquery Datatables. I m using the DataTables hidden row details example but this dosent seem to be working for me.. Can anyone help pls.

Thanks, NC

  • 写回答

1条回答 默认 最新

  • duan02143 2012-10-16 23:36
    关注

    The PHP file is not returning any value, you should generate some JSON output, something like this:

    <?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);
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题