Search code examples
phpformspdomodx

ModX custom package xPDO statement not evaluating correctly


I'm developing a custom package that accepts a form input, compares it against a table and display the relevant information on the screen, I have the transport and schema setup correctly (I hope! I followed the guide at BobsGuides.com) and I can read back from the table without issue. My problems began when I started implementing the form input.

Basically the session variables I am posting never seem to get recieved so the isset never evaluates to true and I just see the form over and over again.

I've only just begun with PHP and am a complete newb when it comes to xPDO so I accept theres probably more than a few things I havn't noticed but if someone could point me in the right direction I'd be extremely grateful.

<?php
    $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;

}

This is my form chunk

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

Solution

  • Give this a try to see if there has been a post submission

    if($_SERVER['REQUEST_METHOD'] == "POST")  
    

    and/or give your form a hidden submit field, some browsers [at least some used to] not post the submit field if you just hit return when the submit button is not in focus. Test for that as well.