如何使用select选项中的数组值作为url中的参数?

dldeef67  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(259)

我有一个下拉菜单,里面有一个sql表中的结果。我想做的是取被选中的选项的值,并将其作为参数添加到url中,这样我就可以从另一个页面调用它。我好像没法让它工作。

$sql = "SELECT name FROM lakes";
$result = mysql_query($sql);

echo "<script>";
echo "var sel = document.getElementById('lakes');";
echo "sel.onchange = function () ";
{
echo "    document.getElementById(\"abc\").href = this.value + \".html\";";
}
echo "</script>";

echo "Lakes in the Frost Centre: ";
echo "<div id=\"lakes\" name=\"lakelist\">";
echo "<select onchange = \"redirect(this.value)\" class=\"required\" data- 
val=\"true\" id=\"lakes\" name=\"lakes\">";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";
}
echo "</select>";
echo "<div class=\"lake-button\">";
echo "    <a id=\"abc\" href=\"lakes/?name=\" + > View Lake </a>";
echo "</div>";
wgmfuz8q

wgmfuz8q1#

假设 $row = mysql_fetch_array($result) 如果没有错误,您应该这样做:

$sql = "SELECT name FROM lakes";
$result = mysql_query($sql);

echo "<script>";
echo "var sel = document.getElementById('lakes');";
echo "function redirect(value) {"; //is already the selected value (this.value)
{
    echo 'document.getElementById("abc").href = "lakes/?name="+value+".html";}';
}
echo "</script>";

echo "Lakes in the Frost Centre: ";
echo '<div id="lakes" name="lakelist">';
echo '<select onchange = "redirect(this.value)" class="d" data- 
val="true" id="lakes" name="lakes">';

while ($row = mysql_fetch_array($result)) 
echo '<option value=' . $row['name'] . '>' . $row['name'] . '</option>';
}

echo "</select>";
echo '<div class="lake-button">';
echo '    <a id="abc" href="lakes/?name=" + > View Lake </a>';
echo '</div>';

请记住 ' 是literal,必须声明onchange调用的函数。
还要记住,如果一些用户选择默认选项(lake1或其他什么),onchange不会触发任何事件。那么你应该包括一些选项,比如 <option>Select whatever..</option> 以触发所有选项的事件。

相关问题