3

I'm trying to make a filter using a MySQL database and PHP for a school project, but whenever I press my filter button, it doesn't execute the action so the querystring is not updated.

The reason it's referring to my homepage is because of this block code:

if (empty($_GET['page'])) {
    $_GET['page'] = 'home';
}

if (empty($routes[$_GET['page']])) {
    header('Location: index.php');
    exit();
}

This is when there is no $GET['page'] passed, and it will just refer to my homepage.

So the problem probably lies with my form action, which is clearly correct: action="index.php?page=agenda"

<form class="filter_form" method="get" action="index.php?page=agenda">
    <div class="location">
        <p class="filter_by">filter by:</p>
        <label for="location">location</label>
        <select name="location" id="location">
            <option value="all">-- Locations --</option>
            <?php foreach($locations as $location): ?>
                <option value="<?php echo $location['location']; ?>"
                    <?php
                        if(!empty($_GET['location'])){
                            if($_GET['location'] == $location['location']){
                                echo ' selected';
                            }
                        }
                    ?>
                  >
                    <?php echo $location['location']; ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>

    <div class="checker_wrapper">
        <div>
            <input type="checkbox" name="check1" id="check1">
            <label for="check1">skills only</label>
        </div>
        <div class="bottom_row">
            <input type="radio" name="group" id="radio1">
            <label for="radio1">day</label>
        </div>
    </div>

    <div class="radio_wrapper">
        <div>
            <input type="checkbox" name="check2" id="check2">
            <label for="check1">in group</label>
        </div>
        <div class="bottom_row">
            <input type="radio" name="group" id="radio2">
            <label for="radio2">evening</label>
        </div>
    </div>
    <input class="button filter_button" type="submit" value="filter" />
</form>

When I press the filter button, I'm in the homepage and the querystring is like this: index.php?location=all, so the $_GET[location] for my project works. It's just not adding the correct page in the string.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arthur Robaeys
  • 327
  • 3
  • 15

4 Answers4

3

Your form is overriding the query string parameters in your action attribute.

Instead of putting the query string in the action, add a hidden field

<form class="filter_form" method="get" action="index.php">
   <!-- ... the rest of your form code -->
    <input class="button filter_button" type="submit" value="filter" />
    <input type="hidden" name="page" id="page" value="agenda" />
</form>

Related post: submitting a GET form with query string params and hidden params disappear

n00dle
  • 5,949
  • 2
  • 35
  • 48
2

You should use a hidden input element in your form rather than adding your parameters in the URL:

<input id="page" name="page" type="hidden" value="agenda">
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
luckygulli
  • 21
  • 4
0

You should add a hidden input field into the form. Instead attach it into the action string,

<input type="hidden" name="page" value="agenda" />
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hung Nguyen
  • 1,026
  • 11
  • 18
0

Add a hidden field:

<input type="hidden" name="page" value="agenda" />

The get parameter in your action is overruled by the method "GET".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gert B.
  • 2,282
  • 18
  • 21