We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1887
    • 50 Posts
    On my site I have a list of installers and I want to add a button for each installer entry that displays a form that can be used to contact the installer via the web site. The idea is that the FormIt template will send the installer an email and CC'd the requestign user (and can logged into our CRM via a hook).

    I have tried to implement it as follows:

    In the listing of installers, the email form link is as follows for each installer:

    <button type="button" 
    onClick="javascript:window.location.href='/shop/installer-email-form.html&installer_id=[[+id]]'" 
    >Send message to Installer »</button>


    which sends the browser to /shop/installer-email-form.html with the ID of the Installer's entry in &installer_id as part of the URL.

    On /shop/installer-email-form.html is a single call:

    [[!pdoResources?
        &parents=`768`     <== this is the parent to all the Installer entries
        &resources=`[[!contact installer retrieve id]]`
        &limit=`1`
        &depth=`1`
        &includeContent=`1`
    	&includeTVs=`installer_email`
    	&showHidden=`1`
    	&processTVs=`0`
        &tpl=`Formit Installer Email`    
    ]] 

    The snippet [[contact installer retrieve id]] returns the installer_id value from the URL as follows:
    <?php
    $element = $_GET['installer_id'];  /* Get the value of the Installer's entry ID  */
    
    if (is_numeric($element)) {         /* Check that it is numeric */
        return $element;
    }
    else
    {
        return "Error";
    } 

    and the pdoResources call loads a tpl which has the FormIt command along with the html form as follows:
    [[FormIt? &hooks=`email,redirect`
    	&redirectTo=`53`
    	&emailTpl=`InstallerContactFormReport`
    	&emailFrom=`info@mycompany`
    	&emailFromName=`My Company Name`
    	&emailTo=`[[+tv.installer_email]]`
    	&emailToName=`[[+pagetitle]]`            <== The installer's company name is stored in the PageTitle
    	&emailSubject=`Sales Enquiry from [[!+first_name]] [[!+last_name]] of [[!+company]]`]]
    

    It all looks like it should work perfectly. Clicking on the "Send Email to the Installer" button takes one to a form with all the variables populated correctly but when the user presses Submit on the form, the email goes to a different installer than the one confirmed in the form. Arruugh!

    What I think happens is that on pressing Submit, /shop/installer-email-form.html is being reloaded but this time the &installer_id has been removed from the URL and pdoResources is picking another installer as it is no longer being limited to a specific one.

    An obvious alternative and much simpler way to implement this is for the template for Installer entries to include the form (so the URL will have changed) but I went for this more complex route as I have different levels on installers and I want some to have their own web pages entries but all installers to be contactable via a web form. This means I can use the Installer resource for their own entry if required.

    How do I pass the &installer_id back to the pdoResources call when the Submit button is pressed or is there a better way of doing this?

    (Site is on modxcloud and running Revolution 2.2.14-pl)

    James
    • What do you get if you run a test case with just [[+tv.installer_email]] in your tpl chunk?

        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
        • 1887
        • 50 Posts
        I have a copy of the FormIt command without the square brackets so I can see the command sequence and all the parameters look right including the installer_email TV.

        It only goes wrong when the form is submitted.
          • 1887
          • 50 Posts
          Thanks for the suggestion. Disabling the redirect option in FormIt made it easier to diagnose.

          I think I have solved it by storing the installer_id in a session variable.

          <?php
          $element = $_GET['installer_id'];  /* Get the value of the Installer's entry ID  */
          
          if (is_numeric($element)) {         /* Check that it is numeric */
              $compare = (int)$element;
              if ($compare > 0) {             /* Check that it is > 0 */
                  $_SESSION['installer_id'] = $element;   /* Store session */
              }
              else
              {
                  $element = $_SESSION['installer_id'];  /* Read session */
              }
          }
          else
          {
              $element = $_SESSION['installer_id'];  /* Read session */
          }
          return $element;