We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44891
    • 35 Posts
    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
    [[GetData]]
    
      • 3749
      • 24,544 Posts
      Try this:

      [[!GetData]]


      That should call the snippet uncached and prevent you from getting the cached results.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 44891
        • 35 Posts
        Quote from: BobRay at Mar 10, 2017, 03:45 PM
        Try this:

        [[!GetData]]


        That should call the snippet uncached and prevent you from getting the cached results.

        Thanks for your reply.
        That didn't do the trick.

        I read a lot about caching pages but the given suggestions didn't give the desired results. Also checked my page settings and made the appropriate adjustments. Still no succes.


          • 44891
          • 35 Posts
          I think I tackeled it myself. I made 2 changes in this line:

          echo '<form method="post" action="' . htmlspecialchars($_SERVER["REQUEST_URI"]) . '">';


          to this:

          echo '<form method="post" action="' . htmlspecialchars($_SERVER["PHP_SELF"]) . '?id=7">';


          As you can see I changed "REQUEST_URI" to "PHP_SELF" and hard-coded the page ID ?id=7
          I'm currently testing this solution thoroughly.