We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13461
    • 2 Posts
    Template variable in eform2db snippet

    What I’m trying to achieve
    I am looking to have a template variable in the eform2db snippet to change the database table name.

    I have a web page with a form snippet and a template variable - [*dbnameTV*]. I want to use this value in the eform2db snippet.

    I can hardcode it in the line $dbQuery = $modx->db->insert($dbTable, databasetablenamehere ); but I want this value to be a variable so I can reuse the form again and again in the same site without creating a duplicate eform2db snippet for each form.


    What I have so far
    Snippet call:
    [!exportclaim? &dbname=`[*dbnameTV*]_table`!]
    [!eForm? &formid=`formname` &from=`` &to=`` &tpl=`form` &report=`form_report` &eFormOnMailSent=`export` &gotoid=`6` &noemail=`1` !]



    Modified eform2db snippet:
    <?php

    function escape($s)
    {return mysql_escape_string($s);
    }

    function export( &$fields )
    {
    global $modx;

    $dbTable = array();


    //example fields
    $dbTable[one] = $modx->db->escape($fields[’one’]);
    $dbTable[two] = $modx->db->escape($fields[’two’]);
    $dbTable[three] = $modx->db->escape($fields[’three’]);
    $dbTable[four] = $modx->db->escape($fields[’four’]);
    $dbTable[five] = $modx->db->escape($fields[’five’]);


    $dbname = $modx->getObject(’modTemplateVar’,’dbnameTV’);

    $dbQuery = $modx->db->insert($dbTable, ’{$dbname}’ );
    return true;
    }
    ?>


    Errors I’m getting
    I get the error PHP Fatal error: Call to undefined method DocumentParser::getObject() regarding the line $dbname = $modx->getObject(’modTemplateVar’,’dbnameTV’);


    Is there any way to the template variable to update the $dbTable value? I’ve picked a few ideas up from http://modxcms.com/forums/index.php?topic=722.0 but getting the TV into the tablename is proving difficult.

    I’m a PHP beginner so any help is appreciated. It’s probably something obvious that I’ve missed but just can’t seem to work it out.

    Note: the page the snippet is used on is has cacheable unchecked (I’ve fallen foul of that one before).