douxun4860 2013-02-09 04:23
浏览 91
已采纳

如何从php文件中的数据库查询获取数组数据到ajax成功数据

i am trying to get the data which is in array, from an SQL query to the ajax success data. Here is the code i tried,

function adminchecksub(value1){
//alert('hi');
var value1=$('#adminsid').val();
//alert(value1);
if(value1==''){
alert('Please enter a Sub id');
}
else{
//alert(value1);
 $.ajax({

                                        type: 'POST',
                                        url: root_url + '/services/services.php?method=adminsubcheck',
                                        data: {value1:value1},
                                        async: true,
                                        success: function (data) {
                                                alert(data);

                                                if (data == 1) {
                                                        alert('inside');
                                                        //window.location.assign(rdurl+"?sid="+sid);
                                                        return true;
                                                } else {
                                                        //alert('does not exist!');
                                                       //alert('outside');
                                                        return false;
                                                }

                                        }
                                });
}

now in the php method which is requested from the above ajax call, i have to get the data return by this function which is returning an array

   function adminsubcheck($sid){
$subid=$sid['value1'];
$row = array();
//echo $subid;
$query = "SELECT Sub_id,Status,Sub_type FROM Sub WHERE Sub_id=$subid";        
        //echo $query;
        //echo $subid;
        $queryresult = mysql_query($query);
        $count  = mysql_num_rows($queryresult);
         //echo $count;

         while ($r = mysql_fetch_assoc($queryresult)) {

           $row[] = $r;
        } 
        return $row;

}

here $row is returning an array as data to the ajax function, where i need to get all the items seperately. Some one help me out to get the values into the ajax call. Thanks in advance..

  • 写回答

3条回答 默认 最新

  • duanchuonong5370 2013-02-09 04:31
    关注

    In your PHP file, you can return your array as a json object in the following way (notice how the PHP file echos the result to pass it back to javascript)

    ...
    $json = json_encode($row);
    echo $json
    

    Now add the following to your javascrip ajax

     $.ajax({
    
    ...
     // Whatever code you currently have
    ...
    
    }).done(function ( json ) {
    
        var data = JSON.parse(json);
    
        //Continue with javascript
    });
    

    You can handle the java script variable 'data' as an associative array like it was in PHP

    also, look how you populate the php variable $row and adjust the following way:

    $cnt = 0;
    while ($r = mysql_fetch_assoc($queryresult)) {
    
            $row[$cnt] = $r;
            $cnt++;
    
    } 
     $json = json_encode($row);
        echo $json;
    

    in javascript you can access your rows the following way in teh data variable:

    var value = data[0]['field_name'];
    

    in the above statement, 0 will correspond to the value of $cnt (i.e. mysql returned row number)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?