0

I have a few filters, search engines, but if you select one or more options, and selecting the action "filter", in selectach are my first choices, and not that on which I filter. How can I do that when you select a particular filter, search after the approval of the filter or filters (reload the page) remembered the filter after which I search?

Code :

<form action="index.php?id=fakturysprzedaz" method='GET'>
<tr><td colspan="2" style="height: 10px;"></td></tr>
<input type='hidden' name='id' value='fakturysprzedaz' >
Typ <select name="type">
<option value="0" name="type" >Wszystko</option>
<option value="1" name="type">Pro-forma</option>
<option value="2" name="type">Faktura VAT</option>
<option value="3" name="type">Korekta faktury VAT</option>
<option value="4" name="type">Faktura barterowa</option>
</select>


<br>
Status<select name="status">
<option name="status" value="0">Wszystko</option>
<option name="status" value="1">Do akceptacji</option>
<option name="status" value="2">Anulowana</option>
<option name="status" value="3">Odrzucona</option>
<option name="status" value="4">Zaakceptopwana</option>
<option name="status" value="5">Wersja robocza</option>
</select><br>
Tansakcja<select name="transakcja">
<option name="transakcja" value="0">Wszystko</option>
<option name="transakcja" value="1">Krajowa</option>
<option name="transakcja" value="2">Zagraniczna</option>
</select><br>
Status Płatności<select name="status-plat">
<option name="status-plat" value="0">Wszystko</option>
<option name="status-plat" value="2">Zapłacono</option>
<option name="status-plat" value="1">Nie zapłacono</option>
</select>
<br>
Kontrahent
<select name="kontrahenci">

<option name="kontrahenci" value="0">Wszystko</option>
<?
$kontrahenciQuery=mysql_query("SELECT * FROM contractors");
while($kontrahenci=mysql_fetch_object($kontrahenciQuery)){
?>
<option name="kontrahenci" value="<?=$kontrahenci->id?>"><?=$kontrahenci->name?></option>
<?
}
?>
</select><br>
Użytkownik<select name="uzytkownik">
<option name="uzytkownik" value="0">Wszystko</option>
<?
$uzytkownicyQuery=mysql_query("SELECT * FROM users");
while($uzytkownicy=mysql_fetch_object($uzytkownicyQuery)){
?>
<option name="uzytkownik" value="<?=$uzytkownicy->id?>"><?=$uzytkownicy->first_name?> <?=$uzytkownicy->last_name?></option>
<?
}
?>
</select><br>
Oddział<select name="oddzial">
<option name="oddzial" value="0">Wszystko</option>
<?
$oddzialQuery=mysql_query("SELECT * FROM departments");
while($oddzial=mysql_fetch_object($oddzialQuery)){
?>
<option name="oddzial" value="<?=$oddzial->id?>"><?=$oddzial->name?> (<?=$oddzial->shortname?>)</option>
<?
}
?>
</select><br>

<tr><td colspan="2" style="height: 10px;"></td></tr>
<tr><td colspan="2" style="height: 30px;"><input type="submit" class="button" name="szukaj" value="Filtruj" /></td></tr>
<?$lata=mysql_query("SELECT DISTINCT YEAR(date_issue) as year FROM invoices_sales");
while($rok=mysql_fetch_object($lata)){
?>
<button type="submit" value="<?=$rok->year?>" name="rok"><?=$rok->year?></button>
<?
}
?>
<?
if(isset($_GET['rok']))
$miesiace=mysql_query("SELECT DISTINCT MONTH(date_issue) as month FROM invoices_sales WHERE YEAR(date_issue)='".$_GET['rok']."'");
else{
$miesiace=mysql_query("SELECT DISTINCT MONTH(date_issue) as month FROM invoices_sales WHERE YEAR(date_issue)='".$_SESSION['a-rok']."'");
}
while($miesiac=mysql_fetch_object($miesiace)){
//print_r($miesiac);
?>
<button type="submit" value="<?=$miesiac->month?>" name="month"><?=$miesiac->month?></button>
<?
}
?>
</form>
Kubol
  • 136
  • 10

2 Answers2

0

When you return to your page, you want your selects to keep the value, right? What you want to do is, to save the state of your select boxes.

These three ways of getting that done come to my mind:

  1. get value of select boxes, serialize it and store them in a cookie

    a nice solution is here

  2. get value and save the state via Ajax to your backend

  3. get value and save the state via Get request to your backend

.

$(document).ready(function() {
  $('#myDropdown').val('<?php echo $_GET['saveFilters']; ?>');
  $('#myDropdown').change(function() {
   location.href = 'http://website.com/backend.php?saveFilters=' + $(this).val();
  });
});

Get value means to get the "selected" state. You get that with $("myDropdown).val(); or $("myDropdown option:selected);.

When the page reloads, use the saved cookie or the saved session data to re-create the proper display.

Community
  • 1
  • 1
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
0

Don't use name tags with the options. Use it only with select. If I got your question right, when you iterate the options add the "selected"-tag for the selected option like this: "<option selected value='1'>Choise 1</option>". The option should be selected if for example the value of $_REQUEST[kontrahenci]-parameter is the same as the value of the iterated row.

viljun
  • 370
  • 3
  • 12