简体   繁体   English

填充下拉菜单(选择)

[英]Populating a Drop Down Menu (select)

I've been trying to solve my question based off of this answer: Populate select box from database using jQuery 我一直在尝试根据以下答案解决我的问题: 使用jQuery从数据库填充选择框

I've attempted to implement what is said in the answers there but I am not having any luck here. 我试图实现那里答案中所说的内容,但是我在这里没有任何运气。 As of now all that appears in the drop down menu is the default "Stone" item that starts in it. 到目前为止,下拉菜单中出现的所有内容都是在其中启动的默认“ Stone”项目。

Could anyone spare some time and give me a hand fixing my issue. 任何人都可以抽出一些时间来帮我解决问题。 My code should essentially read from a MySQL database which has over 150 ID's in order starting at 1 and use the corresponding name in the same ID's row to populate the drop down menu on load. 我的代码本质上应该从一个MySQL数据库读取,该数据库具有150个以上的ID(从1开始),并在相同ID的行中使用相应的名称来填充加载时的下拉菜单。

Example of what drop down menu would look like inside of it: 下拉菜单在内部的示例:

  • Stone
  • Grass
  • Diamond 钻石

What corresponding DB would look like: 相应的数据库将如下所示:

ID           item_name
1            Stone
2            Grass
3            Diamond

The code I'm using to try and do this is: 我用来尝试执行此操作的代码是:

PHP (process_item_list.php): PHP (process_item_list.php):

$con = mysql_connect($DB_HOST,$DB_USER,$DB_PASSWORD);
$dbs = mysql_select_db($DB_NAME, $con);

$tableName = "itemlist";
$result = mysql_query("SELECT * FROM $tableName");

$data = array();
while ( $row = mysql_fetch_row($result) )
{
    $data[] = $row;
}
echo json_encode( $data );    
?>

jQuery/Javascript jQuery / JavaScript

<script type="text/javascript">
$(function(){
      var items="";
      $.getJSON("process_item_lists.php",function(data){
        $.each(data,function(index,item) 
        {
          items+="<option value='"+item.id+"'>"+item.name+"</option>";
        });
        $("#tradeItems").html(items); 
      });
    });
</script>

HTML 的HTML

<select id="tradeItems"> 
<option value="">Stone</option>
</select>

I'm open to different ways to do this as well, as long as it still fills the drop down menu on load! 我也欢迎采取其他方式来做到这一点,只要它仍然可以填充加载的下拉菜单!

Edit: With the help of wirey the PHP issue is fixed. 编辑:在无线的帮助下,PHP问题已修复。 Here is what the results look like from running the PHP file: http://fogest.net16.net/mctrade/php/process_item_list.php When I run the actual page using the alert boxes which should give me the ID and the item name they both return undefined rather then the correct values. 这是从运行PHP文件得到的结果: http : //fogest.net16.net/mctrade/php/process_item_list.php当我使用警告框运行实际页面时,应给我ID和项目名称它们都返回undefined而不是正确的值。

The results at http://fogest.net16.net/mctrade/php/process_item_list.php doesn't look like what you're expecting, it looks like this: http://fogest.net16.net/mctrade/php/process_item_list.php上的结果看起来不像您期望的那样,看起来像这样:

[ ["1","Stone","images\/stone.png"],
  ["2","Grass Block","images\/grass_block.png"],
  /* snip a lot of rows */
]

But what you are expecting is something like this: 但是您所期望的是这样的:

[ { "id":"1", "name":"Stone", "image?":"images\/stone.png"},
  { "id":"2", "name":"Grass Block","image?":"images\/grass_block.png"},
  /* and so on */
]

I think that you'll want to use mysql_fetch_assoc() instead of mysql_fetch_row() , that might give you proper output. 我认为您将要使用mysql_fetch_assoc()而不是mysql_fetch_row() ,这可能会为您提供适当的输出。

Bonus: you can give the response a proper content-type by adding a row before the echo : 奖励:您可以通过在echo之前添加一行来为响应提供适当的内容类型:

header("Content-type: application/json");
echo json_encode($data);

Remark: when I inspected the source of http://fogest.net16.net/mctrade/php/process_item_list.php I found this at the end: 备注:当我检查http://fogest.net16.net/mctrade/php/process_item_list.php的源代码时,我在结尾处发现了这一点:

<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

This should not be part of the response, it will probably make the JSON parser crash. 这不应该是响应的一部分,它可能会使JSON解析器崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM