We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30585
    • 833 Posts
    Hi folks,

    I've put together a FormIt custom hook that saves form data into a custom table. When submitted, the form data is saved successfully and the email report is sent as well. The only problem is the redirect doesn't happen. Once the form is submitted, the page is reloaded; the form reset instead of redirecting to the success page.

    The custom hook:
    <?php
    // Load class
    ...
    
    // Common variables
    // ================
    ...
    $snippet = 'ecSaveQuotes';
    $submit        = $_POST['submit'];
    $submission    = (int) $_GET['submission'];
    
    // Get Form fields
    // ===============
    
    $formFields = $hook->getValues();
    
    $customer_fields = array(
    'firstname' => $formFields['firstname'],
    ...
    );
    $request_fields  = array(
    'cutomer' => $formFields['customerid'],
    ...
    );
    $process_fields = array(
    'request' => $formFields['requestid'],
    ...
    );
    
    // Save the quote and update related objects
    // =========================================
    
    $process  = $modx->newObject('quoteProcess', $process_fields);
    $customer = $modx->getObject('quoteCustomer', $formFields['customerid']);
    $customer->fromArray($customer_fields);
    $request = $modx->getObject('quoteRequest', $formFields['requestid']);
    $request->fromArray($request_fields);
    
    if ($customer->save() && $request->save() && $process->save()) {
      return true;
    } else {
      $modx->log(xPDO::LOG_LEVEL_ERROR, 'An error occured while saving the quote.', '', $snippet, __FILE__, __LINE__);
      $hook->addError('error_message', $error_message);
      return false;
    }
    

    The FormIt call
    [[!FormIt@Estimator?
        &preHooks=`ecLoadRequests` 
        &hooks=`ecSaveQuotes,email,redirect`
        &emailSubject=`Price Estimate`
        &emailTpl=`tplEstimatorEmail`
        &emailFrom=`[email protected]`
        &emailFromName=`[[currentUser]]`
        &emailTo=`[email protected]`   
        &redirectTo=`34`
        &store=`1`
    ]]
    [[$tplEstimatorForm]]

    I'm sure i'm missing something here. Any help will be appreciated.

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

      A MODx Fanatic
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Does it redirect without the post hook?
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 28120
        • 380 Posts
        Assuming the obvious that #34 is not the page the form is on?
          • 28120
          • 380 Posts
          Also is the email hook working?

          What's [[currentUser]] in &emailFromName=`[[currentUser]]`

          Try hard coding the from address to see if that's it?
            • 30585
            • 833 Posts
            @Susan believe it or not, I forgot to check this. It doesn't redirect without the post hook either. The issue is with the prehook. Once it's removed, the form redirects successfully.

            Here's my pre hook snippet:

            Although it's preloading the values as intended, it's also giving me an error. See the final else statemet : No valid submission id supplied. This may be the cause of the problem. I think there is an issue with my main if statement, but I don't know where.

            <?php
            // Load class
            // ==========
            
            ...
            
            $snippet = 'ecLoadRequests';
            $submission = (int) $_GET["submission"];
            
            // Get the existing quote request
            // ==============================
            
            if (!empty($submission)) {
              $request = $modx->getObject('quoteRequest', array(
                'submissionid' => $submission
              ));
              if ($request) {
                // Request object is not null/empty
                // Get request fields
                $requestid           = $request->get('id');
                ...
            
                // Get the related customer + assign placeholders
                $customer   = $request->getOne('Customer');
                $customerid = $customer->get('id');
                ...
                // Set customer placeholders
                $modx->setPlaceholders(array(
                'customerid' => $customerid,
                'firstname' => $firstname,
                ...
                ), 'eq.');
            
                // Set request placeholders
                $modx->setPlaceholders(array(
                'requestid' => $requestid,
                'customer' => $customerid,
                'contentlist' => $contentlist
                ...    
              ), '');
                
            
              } else {
                // Show and log error message if quote request is not found with supplied submissionid
                $hook->addError('error_message', 'Could not find Quote Request with ID: ' . $submission);
                $modx->log(xPDO::LOG_LEVEL_ERROR, 'Could not find Quote Request with ID: ' . $submission, '', $snippet, __FILE__, __LINE__);
                //return false;
              }
            
            } else {
              // Show and log error message if no valid submission id supplied
              $hook->addError('error_message', 'No valid submission id supplied');
              $modx->log(xPDO::LOG_LEVEL_ERROR, 'No valid submission id supplied', '', $snippet, __FILE__, __LINE__);
              //return false;
            
            }
            
            return true;


            @sparky no, the form is not on the redirect page. [[currentUser]] is small snippet that returns the full name of the processing user. The email hook works fine.

            EDIT: I accidentally file this as answered. I still need some help. Can anyone see what may be preventing the redirect when the pre hook is added? [ed. note: treigh last edited this post 11 years, 9 months ago.]
              A MODx Fanatic
            • discuss.answer
              • 30585
              • 833 Posts
              Ok in case this is useful to anyone following this thread, I'm no longer using the pre hook in the FormIt call. I'm calling the snippet before Formit like this:

              [[ecLoadEstimates]]
              [[Formit]]
              [[$tplChunk]]

              The ecLoadEstimates is now successfully preloading form values, the form is submitting and redirecting to the success page.

              Not the solution I was hoping for, but it's effective.
                A MODx Fanatic