We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27442
    • 103 Posts
    I have an &eFormOnBeforeMailSent function that is working fine (inserting into a database table) with one exeption: I want to do a datbase query inside this function. The query works fine outside of the function when run as a standalone snippet.

    When I try to query the database and set a variable, the eForm submission fails and leaves me at a blank page. If I statically assign this variable, it works fine. Please have a look at the code below. What am I missing? Thanks.

    <?php
    //create Packing List Index information
    function addplist( &$fields ){
    
    /*
    ****** This is what I want to do directly, but it doesn't work here ********
    
    	$result = $modx->db->query(' SELECT next_number FROM SalesDept.PackingList_index ORDER BY id DESC LIMIT 1 ');
    	while( $row = $modx->db->getRow( $result ) ) {
    		$creating .= $row['next_number'];
    		}
    		return $creating;
    
    ***** So instead, we call the same thing using runsnippet, which fails the same way ****** 
    
    	$creating = $modx->runSnippet('GetPLNumber');
    
    ***** So next we try the evalsnippet equivalent *********
    
    	$creating = $modx->evalSnippet('GetPLNumber');
    
    ***** Which still fails with a blank page upon submitting *********
    ***** Calling GetPLNumber in a normal document as a standalone snippet works fine *********
    */
    
    // Manually assigning a number works fine
    $creating = 1234;
    echo $creating;
    
    	$new_next_number = $creating+1;
    	$pl_number = 'PL'.$creating;
    
    	//set extra value in fields array
    	$fields['new_next_number']= $new_next_number;
    	$fields['pl_number']= $pl_number;
    
    
    	//Below is ala eForm2db
    	// Init our array
    	global $modx;
    
    	$dbTable = array(); // key = DB Column; Value = Insert/Update value
    
    	// Insert field/value pairs to insert/update in our table
    	$dbTable[entered_by] = $fields['entered_by'];
    	$dbTable[shipto] = $fields['shipto'];
    	$dbTable[po_ref] = $fields['po_ref'];
    	$dbTable[pl_number] = $fields['pl_number'];
    	$dbTable[next_number] = $fields['new_next_number'];
    
    	// Run the db insert query
    	$dbQuery = $modx->db->insert( $dbTable, 'SalesDept.PackingList_index' );		
    
    	return true;
    	}
    
    // Return empty string
    return '';
    ?>
      • 27442
      • 103 Posts
      I’m surprised that all the knowledgeable readers of these forum don’t have any advice. I figured that I was just doing something wrong. Nobody has any ideas how to get around this?