【问题标题】:Passing dropdown list value to another SELECT statement on same page将下拉列表值传递给同一页面上的另一个 SELECT 语句
【发布时间】:2012-08-01 14:00:34
【问题描述】:

您好,感谢您和我一起看这个。我对使用 PHP 运行 MySQL 选择语句完全陌生。话虽如此,我已经成功地运行了一个 SELECT 语句来填充一个下拉列表......以及另一个 SELECT 语句来填充一个 HTML 表。 (这是一个角色扮演游戏)

但这就是我卡住的地方......

我希望下拉选择的值是填充表的第二个选择语句中的“WHERE racename =”值,以便只返回一行而不是所有数据。

这是页面:http://www.gamehermit.com/racechoice.php

到目前为止,这是我的代码:

<?php 

// Make a MySQL Connection 

mysql_connect("localhost", "db_username", "password") or die(mysql_error()); 
mysql_select_db("db_name") or die(mysql_error()); 

$query="SELECT * FROM Races"; 
$result = mysql_query($query); 
echo "<select name=racename>"; 
while($nt=mysql_fetch_array($result)) 
{ 
if ($nt[racename]==$_POST["racename"]) 
$selected="selected"; 
else 
$selected=""; 
echo "<option ".$selected."value=$nt[racename]>$nt[racename]</option>"; 
} 
echo "</select>"; 
echo "<br />"; 

// Get all the data from the "Race" table and create table 

$result2 = mysql_query("SELECT * FROM Races") 
or die(mysql_error()); 

echo "<table border='1'>"; 
echo "<tr> <th>Race Name</th> <th>Might Modifier</th> <th>Valor Modifier</th>         <th>Deftness 

Modifier</th> <th>Insight Modifier</th> <th>Dweomer Modifier</th> </tr>"; 

// keeps getting the next row until there are no more to get 

while($row = mysql_fetch_array( $result2 )) { 
// Print out the contents of each row into a table 
echo "<tr><td>"; 
echo $row['racename']; 
echo "</td><td>"; 
echo $row['modmight']; 
echo "</td><td>"; 
echo $row['modvalor']; 
echo "</td><td>"; 
echo $row['moddeftness']; 
echo "</td><td>"; 
echo $row['modinsight']; 
echo "</td><td>"; 
echo $row['moddweomer']; 
echo "</td></tr>"; 
} 
echo "</table>"; 

?> 

我希望这很简单...非常感谢:)

~杰克

【问题讨论】:

标签: php mysql variables drop-down-menu


【解决方案1】:

最好的方法是使用 AJAX,这样您就不需要传递变量和加载新页面。

但是,您可以使用老式的方法来做到这一点:

假设您将只有一个页面,并且您将选定的值传递给同一页面(需要重新加载页面)

假设你的页面是 game.php

您需要在此页面中包含一个“跳转菜单”,并在用户从列表中选择某些内容后按下提交按钮

在页面的标题中,您需要检查按钮是否被按下

if(isset($_POST['button_name'])) {

// button pressed.. perform next step and select your new data to fill the table

} else {
// nothing pressed and nothing to be performed load the page normally
}

在“if”的“true”中,您需要在这里从列表中获取传递的变量,例如

$var = $_POST['list_name'];

所以现在您有了第二个变量来选择填充表格所需的数据。

完整的代码应该类似于以下,game.php:

<?php 
if(!isset($_POST['go_button'])){ //option not selected display list to choose from
// Make a MySQL Connection 

mysql_connect("localhost", "db_username", "password") or die(mysql_error()); 
mysql_select_db("db_name") or die(mysql_error()); 

$query="SELECT * FROM Races"; 
$result = mysql_query($query); 
$num = mysql_numrows($result);
?>

<script type="text/javascript">
function MM_jumpMenuGo(objId,targ,restore){ //v9.0
  var selObj = null;  with (document) { 
  if (getElementById) selObj = getElementById(objId);
  if (selObj) eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0; }
}
</script>

<form name="form" id="form" action="game.php" method="post">
  <select name="jumpMenu" id="jumpMenu">
  <?php $i=0; while($i<$num) { ?>
    <option value="<?php echo mysql_result($result,$i,'racename_field_value'); ?>"><?php echo mysql_result($result,$i,'racename'); ?></option>
    <?php } ?>
  </select>
  <input type="button" name="go_button" id= "go_button" value="Go" onClick="MM_jumpMenuGo('jumpMenu','parent',0)">
</form>

<?php
echo "<br />"; 
} else { //option selected to get the variable and use it to select data from DB

$var= $_POST['jumpMenu'];

// Get all the data from the "Race" table and create table 

$result2 = mysql_query("SELECT * FROM Races WHERE racename='$var'") 
or die(mysql_error()); 

echo "<table border='1'>"; 
echo "<tr> <th>Race Name</th> <th>Might Modifier</th> <th>Valor Modifier</th>         <th>Deftness 

Modifier</th> <th>Insight Modifier</th> <th>Dweomer Modifier</th> </tr>"; 

// keeps getting the next row until there are no more to get 

while($row = mysql_fetch_array( $result2 )) { 
// Print out the contents of each row into a table 
echo "<tr><td>"; 
echo $row['racename']; 
echo "</td><td>"; 
echo $row['modmight']; 
echo "</td><td>"; 
echo $row['modvalor']; 
echo "</td><td>"; 
echo $row['moddeftness']; 
echo "</td><td>"; 
echo $row['modinsight']; 
echo "</td><td>"; 
echo $row['moddweomer']; 
echo "</td></tr>"; 
} 
echo "</table>"; 
}
?>

我修改了你的代码并添加了一些东西让你开始,如果在尝试加载页面时出现任何错误我没有尝试它就写了它

祝你好运!

【讨论】:

    猜你喜欢
    • 2013-02-09
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多