Question

I have created a website that is displaying bookings from a database. I'm only displaying 10 bookings per page, so I have created a pagination aswell. To get the bookings you have to choose an ID in a select-form. Heres the select-form in page.php?page=1:

<select name="taskOption" required>
    <option value="" disabled selected>Choose ID</option>

    <?php

     if (isset($_POST['taskOption'])) { 
        $Option = $_POST['taskOption'];
       } else {
       $_POST['taskOption'] = 0;
       }

    while($hej < count($hejsan)) {
            if($hej==$_POST['taskOption']){
               $selected = "selected=selected";
             }else{
               $selected = "";
             }
        echo '<option value="' . $hej . '" '.$selected .'>' . $hejsan[$hej] . '</option>';
        $hej++;
    }

    ?>

    </select>
<input type="submit" value="Find bookings">

If I choose an ID in page.php?page=1 and press submit, 10 bookings will appear. When I then press "Next" in my pagination(See under this), page.php?page=2 will show no bookings.

if ($page < $totalPages) {
    echo '<a href="?page='.($page + 1).'"><span>Next</span></a>';
}

I tried this:

if(isset($hejsann[$Option])) {
    echo $hejsann[$Option];
} else { 
    echo "No ID selected!";
}

In page.php?page=1 this will display the ID, when I press "Next" and enter page.php?page=2 it echoes "No ID selected!"

So it seems like $_POST['taskOption'] is not set when I enter page.php?page=2 even though I set the value in page=1. How can I fix this? Thankful for any help!

Was it helpful?

Solution

You can do that by adding id to your pagination and read id from url in next pages like;

$id = (!empty($_POST['taskOption']))? $_POST['taskOption']: $_GET["id"];
if ($page < $totalPages) {
    echo '<a href="?id=' . $id . '&page='.($page + 1).'"><span>Next</span></a>';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top