根据下拉列表的值重新加载 URL 参数
Reload URL with param depending on value of dropdown
我想在下拉列表中进行选择时重新加载带有特殊参数的 URL。实际结果是,URL 发生了变化,我在浏览器栏中看到带有参数的 URL,但页面并没有真正 reload/refresh。
那是我获取 sorttype 参数的代码...实际上没有完成重新加载或刷新,因为其他代码也没有处理
// parse url
$url_components = parse_url($compurl);
// get actual url path
$url = ltrim($url_components['path'], "/"); // ltrim($_SERVER['REQUEST_URI'], "/");
// get parameters from url
$sorttype = "";
parse_str($url_components['query'], $params);
$sorttype = $params['sorttype'];
这就是我放置下拉菜单的代码
<!-- show sortoptions in dropdown-box with redirect on selection change -->
<form action="" method="post" style="float: right;">
<strong>Sortierung:</strong>
<select name="sortmode" onchange="location.href=this.options[this.selectedIndex].value; " style="margin-top:6px;">
<option <?php if (isset($sorttype) && ($sorttype == 'Autor')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Autor' ?>">Autor</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Erscheinungsjahr')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Erscheinungsjahr' ?>">Erscheinungsjahr</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Herausgeber')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Herausgeber' ?>">Herausgeber</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Region')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Region' ?>">Region</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Titel')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Titel' ?>">Titel</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Stadt')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Stadt' ?>">Stadt</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Verlag')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Verlag' ?>">Verlag</option>
</select>
</form>
有多个问题,但主要是误解了 post
和 get
。当你想使用 post
时,你不会更改 url。您只需要使用 $_POST
中设置的变量重新加载页面。我已将代码编辑为最短长度:
<!DOCTYPE html>
<html lang='en'>
<head>
<?php
print_r($_POST);
#print it if want to see if it was set
$sorttype = $_POST['sorttype']; #assign the var
if (!isset($sorttype)){$sorttype="Not sorted";} #to overcome multiple checks
?>
</head>
<form action="" method="POST" style="float: left;">
<strong>Sortierung:</strong>
<select name="sorttype" style="margin-top:6px;" onchange="this.form.submit()">
<option value="Foo" <?php if (($sorttype == 'Foo')) { echo 'selected'; } ?>>Foo</option>
<option value="Bar" <?php if (isset($sorttype) && ($sorttype == 'Bar')) { echo 'selected'; } ?>>Bar</option>
</select>
</form>
</html>
Select 名称是 post 的 var 名称,选择的必须在结束标记之前,如下所示:selected>
。 onchange
只需要提交选择的值this.form.submit()
以下按要求工作,不需要表单标签。
第一行是demo,传入的参数如何使用
<h1>Got <?php echo isset($_GET['sorttype'])? $_GET['sorttype'] : 'no value selected yet'; ?></h1>
<select name="sortmode" onchange="location.href=this.options[this.selectedIndex].value; " style="margin-top:6px;">
<option <?php if (isset($sorttype) && ($sorttype == 'Autor')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Autor' ?>">Autor</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Erscheinungsjahr')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Erscheinungsjahr' ?>">Erscheinungsjahr</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Herausgeber')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Herausgeber' ?>">Herausgeber</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Region')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Region' ?>">Region</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Titel')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Titel' ?>">Titel</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Stadt')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Stadt' ?>">Stadt</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Verlag')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Verlag' ?>">Verlag</option>
</select>
我想在下拉列表中进行选择时重新加载带有特殊参数的 URL。实际结果是,URL 发生了变化,我在浏览器栏中看到带有参数的 URL,但页面并没有真正 reload/refresh。
那是我获取 sorttype 参数的代码...实际上没有完成重新加载或刷新,因为其他代码也没有处理
// parse url
$url_components = parse_url($compurl);
// get actual url path
$url = ltrim($url_components['path'], "/"); // ltrim($_SERVER['REQUEST_URI'], "/");
// get parameters from url
$sorttype = "";
parse_str($url_components['query'], $params);
$sorttype = $params['sorttype'];
这就是我放置下拉菜单的代码
<!-- show sortoptions in dropdown-box with redirect on selection change -->
<form action="" method="post" style="float: right;">
<strong>Sortierung:</strong>
<select name="sortmode" onchange="location.href=this.options[this.selectedIndex].value; " style="margin-top:6px;">
<option <?php if (isset($sorttype) && ($sorttype == 'Autor')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Autor' ?>">Autor</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Erscheinungsjahr')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Erscheinungsjahr' ?>">Erscheinungsjahr</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Herausgeber')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Herausgeber' ?>">Herausgeber</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Region')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Region' ?>">Region</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Titel')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Titel' ?>">Titel</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Stadt')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Stadt' ?>">Stadt</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Verlag')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Verlag' ?>">Verlag</option>
</select>
</form>
有多个问题,但主要是误解了 post
和 get
。当你想使用 post
时,你不会更改 url。您只需要使用 $_POST
中设置的变量重新加载页面。我已将代码编辑为最短长度:
<!DOCTYPE html>
<html lang='en'>
<head>
<?php
print_r($_POST);
#print it if want to see if it was set
$sorttype = $_POST['sorttype']; #assign the var
if (!isset($sorttype)){$sorttype="Not sorted";} #to overcome multiple checks
?>
</head>
<form action="" method="POST" style="float: left;">
<strong>Sortierung:</strong>
<select name="sorttype" style="margin-top:6px;" onchange="this.form.submit()">
<option value="Foo" <?php if (($sorttype == 'Foo')) { echo 'selected'; } ?>>Foo</option>
<option value="Bar" <?php if (isset($sorttype) && ($sorttype == 'Bar')) { echo 'selected'; } ?>>Bar</option>
</select>
</form>
</html>
Select 名称是 post 的 var 名称,选择的必须在结束标记之前,如下所示:selected>
。 onchange
只需要提交选择的值this.form.submit()
以下按要求工作,不需要表单标签。
第一行是demo,传入的参数如何使用
<h1>Got <?php echo isset($_GET['sorttype'])? $_GET['sorttype'] : 'no value selected yet'; ?></h1>
<select name="sortmode" onchange="location.href=this.options[this.selectedIndex].value; " style="margin-top:6px;">
<option <?php if (isset($sorttype) && ($sorttype == 'Autor')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Autor' ?>">Autor</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Erscheinungsjahr')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Erscheinungsjahr' ?>">Erscheinungsjahr</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Herausgeber')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Herausgeber' ?>">Herausgeber</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Region')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Region' ?>">Region</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Titel')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Titel' ?>">Titel</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Stadt')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Stadt' ?>">Stadt</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Verlag')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Verlag' ?>">Verlag</option>
</select>