在这个例子中如何使用javascript而不是PHP

时间:2012-10-18 12:59:36

标签: php javascript ajax implode

大家好我需要将PHP的结果返回给Javascript。

这是我的PHP代码,我需要将其形成为在javascript中使用。

$result = dbMySql::Exec('SELECT Latitude,Longitude FROM data');
while ($row = mysqli_fetch_assoc($result))
   $coordinates[] = 'new google.maps.LatLng(' . $row['Latitude'] . ', ' . $row['Longitude'] . ')';

我需要返回一个数组$坐标并将其破坏,但使​​用Javascript:

var flightPlanCoordinates = [<?php echo implode(',', $coordinates) ?>];

这就是我在javascript中开始的方式:

$.ajax({
        type: 'POST',
        url: 'history.php',
        data: {'query': url},
       });
var flightPlanCoordinates = [<?php echo implode(',', $coordinates) ?>];

我需要用返回的数据填充这个变量,并使用JOIN或其他一些与PHP中的内爆相同的函数。

编辑:

这是我的ajax代码:

$.ajax({
                  type: 'POST',
                  url: 'history.php',
                  data: {
                  'id_user':$('#select-choice-user').val(),
                  'reg_id':$('#select-choice-reg').val(),
                  'd_year1':$('#select-choice-year1').val(),
                  'd_month1':   $('#select-choice-month1').val(),
                  'd_day1':$('#select-choice-day1').val(),
                  'd_year2':$('#select-choice-year2').val(),
                  'd_month2':   $('#select-choice-month2').val(),
                  'd_day2':$('#select-choice-day2').val()
                  },
                  success: function(data)//callback to be executed when the response has been received
                        {

                            for (var i=0;i<data.length;i++)
                            {
                                flightPlanCoordinates[i] = new google.maps.LatLng(data[i].x,data[i].y);
                            }
                        }
                });

这些是JSON格式的返回值:

[
    {
        "x": "46.5564266666667",
        "y": "15.6467166666667"
    },
    {
        "x": "46.5566583333333",
        "y": "15.6465533333333"
    },
    {
        "x": "46.5567416666667",
        "y": "15.6465566666667"
    },
    {
        "x": "46.556745",
        "y": "15.646555"
    },
    {
        "x": "46.5567366666667",
        "y": "15.6465766666667"
    },
    {
        "x": "46.55675",
        "y": "15.6465933333333"
    },
    {
        "x": "46.55677",
        "y": "15.6466116666667"
    },
    {
        "x": "46.5567766666667",
        "y": "15.6466183333333"
    },
    {
        "x": "46.5567783333333",
        "y": "15.64662"
    },
    {
        "x": "46.5567583333333",
        "y": "15.6466066666667"
    },
    {
        "x": "46.556725",
        "y": "15.6465966666667"
    },
    {
        "x": "46.5566983333333",
        "y": "15.6465983333333"
    }
]

我已经检查了JSON验证器并且格式有效。所以我不知道会出现什么问题。

我将PHP代码放在一起,以确保它是按原样编写的:

$result = dbMySql::Exec($query);
while ($row = mysql_fetch_assoc($result))
{
    $coordinates[] = array('x' => $row['Latitude'], 'y' => $row['Longitude']);
}
echo json_encode($coordinates);//send as JSON object

我在控制台中收到的错误类型:

Uncaught Error: Invalid value for constructor parameter 0: undefined   
Uncaught TypeError: Cannot set property '0' of undefined

4 个答案:

答案 0 :(得分:4)

你见过json_encode吗?这应该为你带来魔力。

答案 1 :(得分:1)

好的,阅读评论,似乎应该为var flightPlanCoordinates分配ajax调用的返回值。在那种情况下:

var flightPlanCoordinates = [];
$.ajax({type: 'POST',
        url: 'history.php',
        data: {'query': url},
        success: function(data)//callback to be executed when the response has been received
        {
            for (var i=0;i<data.length;i++)
            {
                flightPlanCoordinates[i] = new google.maps.LatLng(data[i].x,data[i].y);
            }
        }
    });

并且,为了避免eval,只需返回数据:

while ($row = mysqli_fetch_assoc($result))
{
    $coordinates[] = array('x' => $row['Latitude'], 'y' => $row['Longitude']);
}
echo json_encode($coordinates);//send as JSON object

应该做的伎俩

答案 2 :(得分:-1)

var flightPlanCoordinates = <?php echo json_encode($coordinates); ?>

答案 3 :(得分:-1)

<script>  
  var coordinates = new Array();
    <?php
        foreach($coordinates as $key => $value) {
    ?>
        coordinates[<?php echo $key; ?>] = [<?php echo $value; ?>];
    <?php
         }
    ?>
</script>

这将数组从php传递给javascript