简体   繁体   English

从SQL获取数据到下拉列表(HTML中的select \\ option)

[英]Getting data from sql into drop down list(select\option in html)

I am trying to build a drop down element in html, and to add the values according to user pre-choice. 我正在尝试在html中构建一个下拉元素,并根据用户的选择来添加值。 Lets say I have table with one col is game type ans second is a name field. 可以说我有一个col的表是游戏类型,第二个是名称字段。 So if the user first drop down choice is basketball - so another drop down list is opened with all the basketball fields as options. 因此,如果用户的第一个下拉选项是篮球-那么将打开另一个下拉列表,其中所有篮球场均作为选项。

so I have my html file which has inside this php lines: 所以我有我的html文件,其中包含以下php行:

$game_type = $_POST['gameType'];

$con = mysql_connect("localhost", "root", "Jbtraining1");
if (!$con)
    die('Could not connect: ' . mysql_error());

$db_selected = mysql_select_db("test_sport",$con);
$sql = ("SELECT * FROM fields WHERE game_type = '$game_type'");
$result = mysql_query($sql,$con);

while($row = mysql_fetch_array($result))
    echo "<option value='".$row['field_name_en']."'>" . $row['field_name_en'] . "</option>";
?>
</select>

but this lines does not work. 但这行不起作用。 I think most of the lines work well cause if i chage this line: 我认为大多数行都可以正常工作,因为如果我更改此行:

$game_type = $_POST['gameType']; $ game_type = $ _POST ['gameType'];

to let say to this line - 让我们对这句话说-

$game_type = "basketball"; $ game_type =“篮球”;

It does work just fine. 它确实可以正常工作。

Thanks 谢谢

Change your file extension from .html to .php 将文件扩展名从.html更改为.php

Hope this helps 希望这可以帮助

Use 采用

  echo $_POST['gameType']; 

to see if the variable is send. 查看变量是否已发送。

It sounds like it isn't send because you say it works when you use: 听起来好像没有发送,因为您说它在使用时有效:

 $game_type = "basketball";

If the echo is empty something gos wrong when you send the variable. 如果回显为空,则发送变量时会出错。

If that is the case post the code you use to post the variable. 如果是这种情况,请发布用于发布变量的代码。

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

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