We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37258
    • 34 Posts
    I'm really just beginning with PHP programming but I've been working around the language for many years and can find my way around easily enough.

    I'm developing a small addon for modx that simply reads out data from an additional table in the database. I've managed to setup the transport packages using Bobs extremely useful guide for adding custom DB tables. Modx know recognizes the table and I can perform operations on it. All well and good.

    However the addon calls for the user to enter an id in a textfield which is used to pick the row of info to be displayed.

    This is the bit thats causing me issues, for some reason the if statement never evaluates to true so all I ever see is the search form. I spent all day tinkering with the code yesterday with no joy and it would save me a lot of hassle if a coding genius could point me in the right direction!

    Heres my snippet code, its probably miles away from what it should look like, so for that I humbly bow my head in apology.

    $path = MODX_CORE_PATH . 'components/dataease/';
    // get POST variable this is captured
    $accNo = $_POST['accNo'];
    $output = '';
    
    // Check if form has been submitted and that the accNo exists in the db
    if (isset($_POST['submit']) && $_POST['accNo'] == 'accNumber') {
        // Get info from the database
        $query = $modx->newQuery('accno');
        $query->select($xpdo->getSelectColumns('Dataease','Dataease','',array('*')));
        $query->where(array('accNumber:LIKE' => '$accNo'));
        
        // Place it into a variable for output
        $dataease = $modx->getCollection('Dataease',$query);
        // Count the returned rows, should only ever be 1
        $output .= '<p>Total: '. count($dataease) . '</p>';
        
        // Show the found data
        foreach($dataease as $sql) {
            $fields = $sql->toArray();
            $output .= $modx->getChunk('showData', $fields);
    }
        return $output;
    
    } else {
        // Get the form chunk
        $form = $modx->getChunk('dataEntryForm');
        return $form;
    }
    


    This is my form chunk

    <h2>Enter Account Number:</h2>
    <form method="POST" action="">
        <input name="accNo" type="text" value="" />
        <input name="submit" type="submit" value="submit" />
    </form>
    

    This question has been answered by opengeek. See the first response.

      • 6629
      • 60 Posts
      The if statement will only be true if you enter "accNumber" in your form field.

      Just remove
      && $_POST['accNo'] == 'accNumber'
      and it should work.

        • 37258
        • 34 Posts
        Quote from: DasItsch at Feb 21, 2013, 05:24 AM
        The if statement will only be true if you enter "accNumber" in your form field.

        Just remove
        && $_POST['accNo'] == 'accNumber'
        and it should work.


        Thanks for getting back to me so quickly, I tried your suggestion but the page still just displays the form, trying to var_dump the post variables simply shows NULL so it now looks like the form isn't passing the values to the PHP session or the script is being given access to them. Heres my updated code :S

        $path = MODX_CORE_PATH . 'components/dataease/';
        var_dump($_POST['submit']);
        var_dump($_POST['accNo']);
        
        // get POST variable this is captured
        $accNo = $_POST['accNo'];
        $output = '';
        
        
        // Check if form has been submitted
        if (isset($_POST['submit'])) {
            // Get info from the database
            $query = $modx->newQuery('accno');
            $query->select($modx->getSelectColumns('Dataease','Dataease','',array('*')));
            $query->where(array('accNumber:LIKE' => '$accNo'));
            var_dump($query);
            // Place it into a variable for output
            if (!$query) {
                return "Query failed";
            } else {
            $dataease = $modx->getCollection('Dataease',$query);
            // Count the returned rows, should only ever be 1
            $output .= '<p>Total: '. count($dataease) . '</p>';
            
            // Show the found data
            foreach($dataease as $sql) {
                $fields = $sql->toArray();
                $output .= $modx->getChunk('showData', $fields);
            } 
            }
            return $output;
        
        } else {
            // Get the form chunk
            $form = $modx->getChunk('dataEntryForm');
            return $form;
        
        }
        
          • 37258
          • 34 Posts
          So after further investigation it feels like Modx's built in security is blocking my use of session variables somehow.

          I've edited my code to make it much simpler, but when I submit the form the variables are definitely not being passed which means that the if statements conditions never evaluate correctly hence the form is shown again.

          Does anyone know why this could be occuring?
          • discuss.answer
            • 22303 MODX Staff
            • 10,725 Posts
            I assume you are calling your Snippet (or whatever Element Tags you have on the Resource to generate your form) uncached?
              • 37258
              • 34 Posts
              Yeah that was the issue smiley I was kicking myself after I dropped the exclamation mark in and everything started working as intended. Ah well, happy days in the end.