I would like to reload an URL with special parameters when a selection in a dropdown is made. The actual result is, that the URL changes and I see the URL with the parameter in the browserbar, but the page does not really reload/refresh.
Thats the code where I fetch the sorttype param ... in fact a reload or refresh is not done, because other code is also not processed
// 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'];
Thats the code where I place the dropdown
<!-- 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>
There were multiple problems but the main is misunderstanding the post
and get
. When you want to use post
, you don't change the url. You only need to reload the page with your variable set in $_POST
. I have edited the code for minimal length:
<!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 name is the var name for post, selected has to be at the end before closing tag like this: selected>
. onchange
only needs to submit the selected value this.form.submit()