Json在我的列表框中获得“未定义”值

时间:2012-01-23 21:51:34

标签: php mysql json

用户需要从演员列表中单击演员。删除现有的电影片名,然后只播放与所选演员有关的电影片段。

我的“dvdtitle”列表框在选择actor后会收到“Undefined”列表。但是,我使用json_encode检查了来自$('#actor').text(data)函数的响应数据,以使其可视化,并且我确实正确。

[{"503":"Caught In The Crossfire"},
{"690":"Dead Man Running"},
{"1064":"Get Rich Or Die Trying"},
{"1145":"Gun"},{"1254":"Home of The Brave"},
{"2184":"Righteous Kill"},
{"2519":"Streets Of Blood"},
{"3273":"Twelve"}]

我不知道我做错了什么。

//getactors.php
include("includes/connection.php");

$q = $_POST['input'];
$final_array = array();

$resultActor = mysql_query("SELECT id, title, plot, catagory, release_date, rated FROM dvd WHERE actors LIKE '%".$q."%' ");

while($rowActor = mysql_fetch_array($resultActor)) {
    $final_array [] = array( $rowActor['id']   => $rowActor['title'] ); 
}

echo json_encode($final_array); 


// JavaScript Document
$('#actorsname').click(function(){

    var actorValue = $('#actorsname option:selected').text();
    $.ajax({
        type: "POST",
        url: 'getactors.php',
        data: {input: actorValue},
        cache: false,
        datatype: "json",
        success: function(data) {
            $('#dvdtitle option').each(function(i, option) {
                $(option).remove();    // Empty DVD Listbox
            });

            $('#actor').text(data);   //  to visualy check my json Data

            $.each(data, function(i, j) { 
                var row = "<option value=\"" + i.value + "\">" + j.text + "</option>";
                $(row).appendTo("select#dvdtitle");
            });
        }
    });
});

2 个答案:

答案 0 :(得分:4)

问题是你的Ajax正在返回一个包含一个键/值对的对象,其中电影ID作为键,电影标题作为值。这使得检索数据变得更加困难,因为您在each循环中不知道要查找哪个ID。

理想情况下,您将以这种方式格式化JSON:

[
    {"id":503,"title":"Caught In The Crossfire"},
    {"id":690,"title":"Dead Man Running"}
]

这样您就可以使用eachj.id检索j.title循环中的数据,因为JSON使用“id”和“title”作为键。但是因为你没有这样组织,你需要循环遍历对象的每个键/值对。

理想情况下,您需要将PHP更改为:

$final_array[] = array(
    'id' => $rowActor['id'],
    'title' => $rowActor['title']
);

使用j.idj.title(例如var row = "<option value=\"" + j.id + "\">" + j.title + "</option>";)。

以下是不修改PHP代码的示例: 此示例基于上面的示例。 data变量是您在Ajax请求中收到的变量。

// This is the data retrieved using Ajax.
var data = [{"503":"Caught In The Crossfire"},{"690":"Dead Man Running"},{"1064":"Get Rich Or Die Trying"},{"1145":"Gun"},{"1254":"Home of The Brave"},{"2184":"Righteous Kill"},{"2519":"Streets Of Blood"},{"3273":"Twelve"}];

// Loop through each object in the data array.
$.each(data, function(key, movie)
       {
           // Because the ID is a key and the title a value, we can't determine the ID/title unless we loop through each one of the key/value pairs in the object. Each movie only has one key/value pair so this won't be a problem.
           $.each(movie, function(id, title)
                  {
                      $('<option />').attr('value', id).text(title).appendTo('select#dvdtitle')
                  });
       });

jsFiddle:http://jsfiddle.net/j7HGr/

我希望这是有道理的。

PS:您可能还想更改$q=$_POST['input'];以使用mysql_real_escape_string来阻止SQL注入。

答案 1 :(得分:-2)

data是一个对象数组。 i的值是对象的索引,j是对象。

这里:

$.each(x, function(i, j){ 
    $.each(j, function(k, v){
        var row = "<option value=\"" + k + "\">" +v+ "</option>";                
        $(row).appendTo("select#dvdtitle");  
    });   
  });

您可以查看jquery文档以获取更多信息:http://api.jquery.com/jQuery.each/