duanguan1573 2013-11-02 09:25
浏览 67
已采纳

将数据返回到jquery中的ajax调用

I have a php code (no functions, just direct code) which queries a data base stores values in an array and returns the array

<?php
//Query the database and fetch some results
    $array["min_date"] = $arr['min(date)'];
    $array["max_date"] = $arr['max(date)'];
    $array['query'] = $query;

    echo $arr['min(date)'].'</br>';
    echo $arr['max(date)'];

    return $array;
?>

this is my jquery ajax call

function date(){
    $temp = $('select[name=people_name]').val();
    $name = $temp;
    $table = 'myTablename'; 
    $url = "/myurl/php/get_date.php?name="+$name+"&table="+$table;
    $.ajax({
        type: "POST",
        url: $url,
        success: function(data) {
         document.getElementById("from_date").value = data['min_date'];
         document.getElementById("to_date").value = data['max_date'];
        }
    });
}

when I echo the php variables I do get the data which I want. but logging the jquery variables the give me result as undefined.

maybe the php return data is not fetches by ajax success(data)? or do I need to have a function in my php code? how do I fetch returned array in my jquery?

Thanks!

  • 写回答

2条回答 默认 最新

  • dongxu6418 2013-11-08 09:00
    关注

    In your PHP

    //Query the database and fetch some results
    $array["min_date"] = $arr['min(date)'];
    $array["max_date"] = $arr['max(date)'];
    $array['query'] = $query;
    echo json_encode($array); //add this
    

    In ajax call

    function date(){
        $temp = $('select[name=people_name]').val();
        $name = $temp;
        $table = 'myTablename'; 
        $url = "/myurl/php/get_date.php?name="+$name+"&table="+$table;
        $.ajax({
            type: "POST",
            dataType:'json', //add dataType
            url: $url,
            success: function(data) {
             document.getElementById("from_date").value = data.min_date;
             document.getElementById("to_date").value = data.max_date;
            }
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示