How Can I send $_POST with query string in a link

2019-09-10 06:11发布

问题:

This is php code which checks if form(on same page) is posted and gets the result depending upon $_POST array if it is posted or returns all results from data base. this also sets $total variable containing number_of_pages later used for pagination

if(!isset($_POST['search']))
{

$id = (isset($_GET['id']) ? $_GET['id'].'0' : 10);
$id = $id-10;
$query  = "select SQL_CALC_FOUND_ROWS * from feedbacks order by Date desc limit $id, 10 ";
}
else if (!empty($_POST['from']))
{

$timstmp = strtotime($_POST['from']);
$dt = date("Y-m-d H:i:s", $timstmp);

$id = (isset($_GET['id']) ? $_GET['id'].'0' : 10);
$id = $id-10;
$query = "SELECT SQL_CALC_FOUND_ROWS * FROM `feedbacks` WHERE `Date` >= \"$dt\" order by Date desc limit $id, 10 ";
}
else if (!empty($_POST['name']))
 {

$name = '%'.$_POST['name'].'%';

 $id = (isset($_GET['id']) ? $_GET['id'].'0' : 10);
$id = $id-10;
$query = "SELECT SQL_CALC_FOUND_ROWS * FROM `feedbacks` WHERE `name` LIKE  '$name' order by Date desc limit $id, 10 ";
}
 $result= mysqli_query($connection, $query);

$r =  mysqli_fetch_array(mysqli_query($connection, 'SELECT FOUND_ROWS()'));
$total = ceil($r['FOUND_ROWS()']/10);

$feedbacks = array(); 
for ($i = 0;$row= mysqli_fetch_assoc($result); $i++)
{
    $feedbacks[$i] = new FeedBack($row);
}


?>

These are the html links that are automatically generated depending upon the total number of pages which is calculated depending upon results from data base

<?php
if ($total>1)
{ ?> <div class="pagging">
<?php     for( $i=$total; $i>=1; $i--)
{ ?>

                        <div class="right">
                            <a id="pagination" href="index.php?id=<?php echo $i; ?>"><?php echo $i; ?></a>
                        </div>

                    <?php } ?>
                     </div>
            <?php       } ?>
                </div>
                <!-- Table -->

On this page I'm implementing filters. User input data in the form and press search so depending upon the $_POST array the different results are loaded and variable $total containing num_of_pages is modified as the 1st block of php code shows now the first page displays 1st 10 results from data base using offset 0 or ofset = IDX10-10 if id is set but problem is that it only works for default results i.e when all results are displayed and filter is not applied but when user searches through a filtered 1st 10 filtered results are displayed but when user clicks the next page it does not work because $_POST is unset which was checked by the php If conditions in above php code. so my question is how can i send $_POST[] along with the id to make it work. Any help would be appreciated. Thanks.

回答1:

You can't send POST variables through a link. You need to use GET.

<a href="some_page.php?<?=http_build_query($assc_array);?>">Link</a>
//the link will reflect some_page.php?name1=value1&name2=value2...etc

On the PHP side use the $_GET or $_REQUEST variable, instead of $_POST.