ajax返回undefined,控制台在firebug只响应一行数据

时间:2012-11-27 07:03:53

标签: php ajax

嘿家伙我试图通过json_encode获取我的ajax的值并使用$ .each迭代每个结果但每个输入从我的php返回未定义的值这里是ajax

var counter_sub = 0 ;
var html;
          $.ajax({
                    type:'POST',
                    url:'add_subject.php',
               dataType:'json',
                   data:{'func_numbr':'2'},
                success:function (data){
                   $.each(data, function(i, item) {

                    html = "<tr>";
                    html += "<td><lable>Subject: </label><input type='text' name='subject["+counter_sub+"]' value='"+data[i].subj_name+"'></td>";
                    html += "<td><input type='button' id='activate' name='active' class='button' value='Active'> ";
                    html += "<input type='button' id='inactivate' name='active' class='button' value='Inactive'></td>";
                    html += "</tr>";
           $('#curr-elem-tble').append(html);

           counter_sub = counter_sub +1;
          });
                }     
         });

和php

<?php
include_once('DBconnect.php'); 
$func_num = $_POST['func_numbr'];

switch ($func_num) {
case 1:
    insert_new_subject();
    break;
case 2:     
    get_elementary_subjects();  
    break;
 }

function insert_new_subject(){
 $level = $_POST['year_level'];
 $subjct_name =$_POST['subjct_name'];
 $units_nmber = $_POST['subjct_units'];

 $insert_subject = "INSERT INTO subjects (subject_name,level,status,subject_units)     VALUES('$subjct_name','$level','1','$units_nmber')";

                if(@!mysql_query($insert_subject)){
                    die('error insert'.mysql_error());

                    }
                    else{
                        $return ['error'] = false;
                    }

echo json_encode($return);                  
}

  function get_elementary_subjects(){

$select_subjects_elem = "SELECT subject_name,subject_id,status FROM subjects WHERE   level='elementary' "; 

    $connect = mysql_query($select_subjects_elem) or die(mysql_error());
    $data[]=array();

    while($row = mysql_fetch_array($connect)){
        $data= array(
            'subj_name' =>$row['subject_name'], 
            'subj_id'=>$row['subject_id'],
            'subj_status'=>$row['status'],
            );
    }

    echo json_encode($data);
}
?>

我将每个td切成不同的html行,然后用+ =连续不知道为什么它返回undefined。我用firebug检查它,控制台只返回1行数据,虽然它遍历所有结果。

2 个答案:

答案 0 :(得分:2)

你只得到一行,因为在你的while循环中,你覆盖整个数组而不是附加到它。将数组声明更改为:

    $data = array();

和循环中的代码:

    $data[] = array(
        'subj_name' =>$row['subject_name'], 
        'subj_id'=>$row['subject_id'],
        'subj_status'=>$row['status'],
    );

答案 1 :(得分:0)

试试这段代码,

function get_elementary_subjects(){

    $select_subjects_elem = "SELECT subject_name,subject_id,status FROM subjects WHERE   level='elementary' "; 

    $connect = mysql_query($select_subjects_elem) or die(mysql_error());

    $data = array(); //<---changed $data[] to $data

    while($row = mysql_fetch_array($connect)){

        //change $data to $data[] 
        $data[] = array(
            'subj_name' =>$row['subject_name'], 
            'subj_id'=>$row['subject_id'],
            'subj_status'=>$row['status']  //<---removed comma here

        );
    }

    echo json_encode($data);
    exit; //<---Added exit here
}

在上面的代码中查看我的评论