数组推入PHP,但ajax返回undefined

时间:2013-02-02 12:47:58

标签: php jquery ajax

我试图使用php

在我的数组中推送新值
$select_all_schools = "SELECT * FROM schools ";
$query = mysql_query($select_all_schools) OR die(mysql_error());

while($row = mysql_fetch_array($query)){
    $item=array(    
        'school_name' => $row['school_name'],
        'school_address' => $row['school_address'],
        'school_id' => $row['school_id'],
    );

    $select_sections = "SELECT * FROM section WHERE school_id = '".$row['school_id']."'";

    $query_section = mysql_query($select_sections) or die(mysql_error());
    $sections_counts = mysql_num_rows($query_section);
    $select_sections_deped_archive = "SELECT * FROM deped_grade_archive 
                                      WHERE school_id = '".$row['school_id']."'
                                      GROUP BY section_id ";

    $query_section_deped_archive = mysql_query($select_sections_deped_archive) or die(mysql_error());
    $sections_counts_grade_archive = mysql_num_rows($query_section_deped_archive);

    if($sections_counts_grade_archive == $sections_counts ){
         $item['stat'] = 'Complete';
    }
    else{
        $item['stat'] ='Incomplete';
    }
}           

echo json_encode($item);

然后使用ajax

获取值
function get_all_school_status(){
    $.ajax({
        type:'POST',
        url:'deped_functions.php',
        dataType:'json',
        data:{'func_num':'1'},
        success:function (data){
            $.each(data, function(i, item) {
               html = "<tr>";
               html += "<td style='width:20%;'><input type='radio' name='school_id' value='"+data[i].school_id+"'></td>";
               html += "<td style='width:25%;'><label>"+data[i].stat+"</label></td>";
               html += "<td style='width:55%;'><label >"+data[i].school_name+"</label></td>";
               html += "</tr>";

               $('#table-schools-content').append(html);
           });
       }
   });
}
get_all_school_status();

但不幸的是我得到了未定义的值,虽然我的控制台显示我正确地从php获取值到ajax。我做错了什么?..请帮助伙计们。 tnx提前

2 个答案:

答案 0 :(得分:1)

Js部分代码是正确的,但你的 php数组错误 尝试替换上面的代码。这些错误被评论了。

$count = 0; // keys of array
while($row = mysql_fetch_array($query)){
    $count++; // in prevoius version you get only one row from table
    // correct version of school array
    $item[$count] = array(    
        'school_name' => $row['school_name'],
        'school_address' => $row['school_address'],
        'school_id' => $row['school_id'],
    );

    $select_sections = "SELECT * FROM section WHERE school_id = '".$row['school_id']."'";

    $query_section = mysql_query($select_sections) or die(mysql_error());
    $sections_counts = mysql_num_rows($query_section);
    $select_sections_deped_archive = "SELECT * FROM deped_grade_archive 
                                      WHERE school_id = '".$row['school_id']."'
                                      GROUP BY section_id ";

    $query_section_deped_archive = mysql_query($select_sections_deped_archive) or die(mysql_error());
    $sections_counts_grade_archive = mysql_num_rows($query_section_deped_archive);

    if($sections_counts_grade_archive == $sections_counts ){
        // success or error message with key 'status'
        $item[$count]['status'] = 'Complete';
    }
    else{
         $item[$count]['status'] = 'Incomplete';
    }
}

结果如下: http://clip2net.com/s/2MJmt

答案 1 :(得分:-1)

jQuery .each method并不意味着用于遍历数组而是HTML元素。应该使用FOR循环正常遍历您的学校阵列:

success: function(data) {
               if(typeof data == "string")   //handle PHP errors - this happens if non-json string is returned by a server
                   alert("JQuery failed to convert data to JS object!");
               for(var i=0; i<data.length; i++) {
                   /*Do whatever with data[i]*/
               }
         }

如果数据数组包含非*有序键,如data [“school1”]或类似的东西,则必须使用类似于PHP foreach的for(var i in data)

另外,我不确定,但我认为你应该dataType:'application/json',这是正确的json mime type