php - 使用 AJAX 返回表中的所有记录

标签 php javascript mysql

我有一个程序,它通过 AJAX 发送用户 ID 到一个 php 程序,该程序在表中搜索与该用户 ID 匹配的所有记录。问题是它只返回一行,我需要它返回所有匹配的行。关于如何做到这一点有什么想法吗?

PHP 中的 MySQL 语句:

$id=$_POST["id"];

$sql = mysql_query("SELECT * FROM crime_map WHERE user_id = '$id'");
while($row = mysql_fetch_assoc($sql))

$output[]=$row;

echo json_encode($output);

JS代码:

function submitform() {

    var id = document.getElementById('id').value;
    var datastr = 'id=' + id;
    alert(datastr);
    $.ajax({    
        type: "POST",
        url: 'api.php', 
        data: datastr,      
        dataType: 'json',                   
        success: function(data){

            var user_id = data[0];
            alert(user_id);
        }                                   
    })      
}

最佳答案

function submitform() {

    var id = document.getElementById('id').value;
    var datastr = 'id=' + id;
    //alert(datastr);
    $.ajax({    
        type: "POST",
        url: 'api.php', 
        data: datastr,      
        dataType: 'json',                   
        success: function(data){

            //var user_id = data[0];
            //alert(user_id);
            if(data.length > 0)
            {
                for(i=0; i<data.length; i++)
                {
                    alert("User: " + data[i].user_id);
                    // here you have the user_id and any other fields from the table e.g. lat/long
                }
            }
        }                                   
    })      
}

查看 Google map 折线引用,了解如何绘制点: http://code.google.com/apis/maps/documentation/javascript/reference.html#Polyline

关于php - 使用 AJAX 返回表中的所有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9614044/

相关文章:

c# - 在 updatepanel 刷新时更改来自 asp.net 代码隐藏的 javascript 变量

php - 定时账户访问(PHP/mysql)

mysql - 查询 : How to get data from another row to fill lacking data?

mysql - 如何在 MySQL 查询中进行正则表达式

php - 带有 Composer 的 Google API PHP 客户端

php - AJAX POST 返回数据未出现

javascript - 从 asp.net mvc 中的 _Layout 页面 Ajax 搜索弹出窗口

PHP 错误 : Function name must be a string in

php - CodeIgniter 将带有查询的数组传递到 View 中

javascript - 通过 jquery/jscript 重命名 WordPress 文件夹中的文件