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

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画