所以我已经有了一个从MySQL数据库生成的下拉列表。我无法解决的问题是如何显示所选选项的"预览"。
这是我正在构建的游戏的一部分,你可以去"竞技场"。在竞技场上,会有一个对手的下拉列表可供选择,所有对手都有不同的价值观,比如名字、级别、实力等等。因此,如果你从列表中选择"对手1",我希望显示对手预览,如果我将下拉菜单更改为"对手2",我则希望更新预览。当玩家决定与该对手战斗时,可以通过点击我的"战斗"按钮进行提交。
这是我正在使用的下拉列表:
编辑:请参阅下面的我的简历和更新的代码。
在扯了我8个小时的头发后,谷歌和ALOT的阅读。。我设法把这些放在一起,它正在发挥作用!
HTML代码:
<div id='load_npc'>
<table width='720' border='0' align='center'>
<td align='center'>
<form id='form1' name='form1' method='post' action=''>
<fieldset>
<legend><strong>Pick your opponent!</strong></legend>
<p>
<select name='npc_name' id='npc_name'>
<option value='' selected='selected'> Select an opponent.. </option>
<option name='row[name]' value='row[name]'><php_row></php_row> </option>
</select>
<br>
<input type='submit' value='Fight!' '>
</p>
</fieldset>
</form>
</td>
</table>
<div id='results'></div>
</div>
Javascript代码,放在标题中:
$(function() { // document.ready
$("#npc_name").on("change", function() {
$.ajax({
url: "system/npc_load.php",
type: "POST",
data: {
npc_name: $(this).val()
},
success: function(data) {
$("#results").html(data);
}
});
});
});
这是我的脚本引用的php文件:
<?php
require ("database.php");
$npc_name = $_POST['npc_name'];
$sql="SELECT * FROM npc WHERE name = '" . $npc_name . "'";
$myData = mysqli_query($connection, $sql);
//count if there are any results
$numrow = mysqli_num_rows($myData) or die(mysqli_error($connection));
if($numrow == 0)
{
echo "No results found.";
}
else
{
echo '<fieldset><legend><strong>Information</strong></legend><p>
<table width="auto" height="172" border="0">
<tr><th>Name</th></tr>
<tr><th>Type</th></tr>
<tr><th>Level</th></tr>';
while($info = mysql_fetch_array($myData))
{
echo "<form action='system/npc_load.php' method='post'>";
echo"<tr>";
echo "<td align='center'>" . $info['name'] . "<input type=hidden name=name value=" . $info['name'] . " </td>";
echo "<td align='center'>" . $info['type'] . "<input type=hidden name=type value=" . $info['type'] . " size='11' maxlength='11' /> </td>";
echo "<td align='center'>" . $info['level'] . "<input type=hidden name=level value=" . $info['level'] . "' /> </td>";
echo "</tr>";
echo "</form>";
}
}
echo "</fieldset>";
?>