0

get を使用してボタンから情報を取得し、配列を吐き出そうとしています。これまでのところ、このコードを実行して.getJSONも は呼び出されません (クエリを取り出すと動作しますが、クエリに問題は見つかりません)。

関連するコードは次のとおりです。

Javascript:

$.getJSON("/westcoast_map.php", // The server URL
{
    westcoast_id: $('.opener').data('westcoast_id')
}, // Data you want to pass to the server.
function (json) {

    var id = json[0];
    waypts = json[1];
    alert(id);
    console.log(waypts);

});

PHP スクリプト(westcoast_map.php):

<?php
session_start();
require_once "database.php";
db_connect();
require_once "auth.php";
$current_user = current_user();
include_once("config.php");

$westcoast_id    = $_GET['westcoast_id'];
$westcoast_array = array();
$query           = "SELECT city, state FROM users GROUP BY city";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {

    if ($row['city'] != '' && $row['state'] != '') {
        $westcoast_array[$row] = "{location:" . $row['city'] . ", " . $row['state'] . ", stopover:true}";
    }
}
$data = array(
    $westcoast_id,
    $westcoast_array
);
echo json_encode($data);
?>

ボタン:

<button type="button" class="opener" data-westcoast_id="westcoast_id" onclick="calcRoute();">
    West Coast
</button>
4

2 に答える 2

1

keyあなたがあなたの状態に欠けているように私には見えますvalue、これを試してください:

if($row['city'] != '' && $row['state'] != '') {
    $westcoast_array[] = "{
        location:".$row['city'].", 
        state:".$row['state'].", 
        stopover:true
    }";
}

また、配列をキーとして使用していますが、これはできません。$westcoast_array[$row]に変更$westcoast_array[]

于 2013-03-01T21:43:07.407 に答える
0

私が間違っていたら申し訳ありませんが、あなたは自分で作って、それを再びエンコードするためにJSON使用していると思います。json_encodeこれを試して:

<?php
session_start();
require_once "database.php";
db_connect();
require_once "auth.php";
$current_user = current_user();
include_once("config.php");

$westcoast_id    = $_GET['westcoast_id'];
$westcoast_array = array();
$query           = "SELECT city, state FROM users GROUP BY city";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {

    if ($row['city'] != '' && $row['state'] != '') {
        $westcoast_array[] = array("location" => $row['city'], "state" => $row['state'], "stopover" => true);
    }
}
$data = array(
    $westcoast_id,
    $westcoast_array
);
echo json_encode($data);
?>
于 2013-03-08T15:01:32.577 に答える