Input form like
<form action="<?php echo (htmlspecialchars($_SERVER["PHP_SELF"] .'?page=1')) ?>" method="post">
<input type='text' name='date_month' value='<?php echo $_POST['date_month'].$_GET['date_month']?>'>
</form>
Then receive the input
$post_date_month = $_POST['date_month'].$_GET['date_month'];
Based on the input select/get filtered data from mysql
As there may be many rows of data, created pagination. Like this
for ($page_i = 1; $page_i <= $total_pages; $page_i++) {
echo "<a href='__filter_mysql_data.php?page=$page_i&date_month=$post_date_month&date_year=$post_date_year'>| $page_i |</a> ";
}
Note! If in url I do not use &date_month=$post_date_month
then when user clicks on page number $page_i
, script will display all data from mysql (not based on $post_date_month
; not only month selected by user, but all months because after page reload $_POST['date_month']
"disappears").
Now this is the only working solution for me (have found several examples with pagination class, but these examples are very complicated for me so can not in reasonable time ammend for my needs). So decided to create something simple.
The script works as necessary, only this $post_date_month = $_POST['date_month'].$_GET['date_month'];
looks a bit 'crazy'. So the question: is the code acceptable (can be used in scripts)? With the same query (click) user can set/pass either $_POST or $_GET and never the both.
Update
Based on Ø Hanky Panky Ø advice (that unfortunatelly is deleted) changed
$post_date_month = $_POST['date_month'].$_GET['date_month'];
to
$post_date_month = $_REQUEST['date_month'];
htmlspecialchars
to prevent XSS when outputting user data. – Marcel Korpel Jun 17 at 16:57$_REQUEST
. php.net/manual/en/reserved.variables.request.php – Crontab Jun 17 at 16:57htmlspecialchars
is used in '<?php echo (htmlspecialchars'. I will check other places.... – user2466952 Jun 17 at 17:02value='<?php echo $_POST['date_month'].$_GET['date_month']?>'
, not indate_month=$post_date_month
(where you shouldurlencode
the parameter) and don't put a space between the@
and my name to call me properly. – Marcel Korpel Jun 17 at 17:04$_GET['blah']
containsfoo
,$_POST['blah']
containsbar
, what will$_REQUEST['blah']
contain? Is itfoo
,bar
,foobar
orbarfoo
? – Marcel Korpel Jun 17 at 17:07