I try to pull some data from my database. The layout of my form is simple. At the top the alphabet and then a form where you can enter a word to search for.
Almost everything works fine but . . .
When I click on a letter (at the top) from the alphabet the query delivers the desired results. No problem here. The URL looks like it should:
https://www.bertverberne.com/modx/index.php?id=7&letter=D
The parameter in the url is visible: &letter=D
But when I then enter a word in the searchbox it repeats the results from the above query.
Only when I reset the page to:
https://www.bertverberne.com/modx/index.php?id=7
and then enter a word in the searchbox it delivers the results I want. For example try:
odbc
and hit enter (or click Search).
Or try:
input
and hit enter (or click Search).
I noticed that the url in the addressbar doesn't show the parameter now (the word you are looking for).
Results are there. Even as I go back to alphabet and click a letter it produces the appropriate results. But switching back to the searchbox it repeats the results from when I clicked the letter earlier.
I want to get the desired results from both search options (alphabet or searchbox)
What can be wrong here? Looks like the page doesn't reset in some way.
Just for the sake of clarity, beneath you find all the code I've got.
php-snippet:
<?php
// Show all error messages
error_reporting(E_ALL);
// Display alphabet
echo 'post_title start with:<br /><br />';
$arrList = array_merge(array(
'0-9'
) , range('A', 'Z'));
foreach($arrList as $value)
{
echo '<a href="https://www.bertverberne.com/modx/index.php?id=7&letter=' . $value . '">' . $value . '</a> ';
}
// Start form
echo '<br /><br />';
echo '<form method="post" action="' . htmlspecialchars($_SERVER["REQUEST_URI"]) . '">';
echo '<input type="text" name="zoekvak" />';
echo ' Search in post_title';
echo '<br /><br />';
echo '<input type="text" name="author" />';
echo ' Search in author';
echo '<br /><br />';
echo '<input type="checkbox" name="sort_by_date" value="1" />';
echo ' Sort on date';
echo '<br /><br />';
echo '<input type="submit" value="Search" />';
echo '<br />';
echo '</form>';
echo '<br />';
// End form
// Empty variables
$zoekvak = $auteur = "";
// Check fields with function test_input()
if ($_SERVER["REQUEST_METHOD"] == 'POST')
{
$zoekvak = test_input($_POST['zoekvak']);
$auteur = test_input($_POST['author']);
}
if (isset($_POST['sort_by_date']) && $_POST['sort_by_date'] == 1)
{
$sorteren = 1;
}
else
{
$sorteren = 0;
}
// Check fields with function test_input()
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// Connect to database
Try
{
$dbh = new PDO('mysql:host=myhost;dbname=mydatabase', 'user', 'mypw');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo '<pre>';
echo 'Regel: ' . $e->getLine() . '<br />';
echo 'Bestand: ' . $e->getFile() . '<br />';
echo 'Foutmelding: ' . $e->getMessage();
echo '</pre>';
}
if (isset($_GET['letter']))
{
$sql = $dbh->prepare("SELECT * FROM wp_posts WHERE post_title LIKE ? ORDER BY post_date");
$sql->execute(array("$_GET[letter]%"));
}
else
{
$zoekvak = trim($zoekvak);
$sql = $dbh->prepare("SELECT * FROM wp_posts WHERE post_name LIKE ? ORDER BY post_date");
$sql->execute(array("%$zoekvak%"));
}
echo '<table id="example" class="display" cellspacing="0" width="100%"><thead><tr><td>Nr.</td><td>Id</td><td>Date</td><td>post_title</td><td>published</td><td>Author</td><td>post_name</td></tr></thead><tbody>';
$counter = 1;
// Show results
while ($resultaat = $sql->fetch(PDO::FETCH_ASSOC))
{
echo '<tr><td>';
echo $counter;
echo '.</td>';
echo '<td>' . $resultaat['ID'] . '</td>';
echo '<td>' . $resultaat['post_date'] . '</td>';
echo '<td>' . $resultaat['post_title'] . '</td>';
echo '<td>' . $resultaat['post_status'] . '</td>';
echo '<td>' . $resultaat['post_author'] . '</a></td>';
echo '<td>' . $resultaat['post_name'] . '</a></td></tr>';
$counter = $counter + 1;
}
echo '</tbody></table>';
//Close connection
$dbh = NULL;
?>
Template code
<!doctype html>
<html lang="en">
<head>
<title>[[*pagetitle]] - [[++site_name]]</title>
<base href="[[!++site_url]]" />
<meta charset="[[++modx_charset]]" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css">
</head>
<body>
<h1>[[*longtitle:default=`[[*pagetitle]]`]]</h1>
[[*content]]
</body>
</html>
The resource which is
https://www.bertverberne.com/modx/index.php?id=7