We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38181 ☆ A M B ☆
    • 26 Posts
    Hi, ay help appreciated.

    The summary of the problem : I have a FormIT form with various fields, some of which are drops downs populated by snippets to connect to MySQL to get the list of entries. I use the FormIT validation process. All is working fine but for one thing : if the user gets some data entry wrong (for example the they enter an invalid email address) such that they get returned to the form, then the data that they had selected in the drop downs on the page is not 'selected' so they have to go through all the selections of drop downs again. How do I retain the selections they made?

    So here is what I have:

    1. The FormIt call with the validation in (just removed my email address for here)

    [[!FormIt? &hooks=saveProperty,email,redirect &emailTo=`[email protected]` &redirectTo=`36` &validate=`address1:required,address2:required,town:required,telephone:required,contactname:required,contactemail:email:required` &store=`1` &emailTpl=contactTpl]]

    2. The list boxes on the page are populated using snippets calls : e.g. below (there are several)

    <tr>
      <td><br/><span class="error">[[+fi.error.address3]]</span></td>
      <td><input id="address3" type="text" name="address3" value="[[+fi.address3]]" size="45" /></td>
      <td>Location:</td><td>[[!ddLocations]]</td>
    </tr>


    3. The snippet looks likes this :

    <?php
    $reg = $modx->getService('sflocations','SFLocations',$modx->getOption('sponsorfinder.core_path',null,$modx->getOption('core_path').'components/sponsorfinder/').'model/sflocations/',$scriptProperties);
    if (!($reg instanceof SFLocations)) return 'Failed to getService for SFLocations'; 
    $sql = 'SELECT id,location FROM modx_sflocations';
     
    $output ='<select id="location"><option>Please select...</option>';
    
    $m = $modx->getManager();
    
    $locations = $modx->getCollection('SFLocation');
    if ($locations && count($locations)>0) {
      foreach ($locations as $location) {
        $locationArray = $location->toArray();
        if ( $scriptProperties['location'] == $locationArray['id'] )
          $output .= ' selected="yes"';
        $output .='<option value="'.stripslashes($locationArray['id']).'">'.stripslashes($locationArray['location']).'</option>';
      }
    }
    
    $output .='</select>';
     
    return $output;


    But the code that should look to insert 'selected = 'yes' against the appropriate selected option appears not be being fired.

    Any ideas .... or an alternative solution greatly appreciated.

    Cheers for now.
      • 38181 ☆ A M B ☆
      • 26 Posts
      The FormIt code got missed .... it looks like this ....

      [[!FormIt? &hooks=saveProperty,email,redirect &emailTo=`[email protected]` &redirectTo=`36` &validate=`address1:required,address2:required,town:required,telephone:required,contactname:required,contactemail:email:required` &store=`1` &emailTpl=contactTpl]]
        • 38181 ☆ A M B ☆
        • 26 Posts
        And sorry again!! The snippet code above was not quite right : a typical piece looks like this:

        <?php
        $reg = $modx->getService('sfages','SFAges',$modx->getOption('sponsorfinder.core_path',null,$modx->getOption('core_path').'components/sponsorfinder/').'model/sfages/',$scriptProperties);
        if (!($reg instanceof SFAges)) return 'Failed to getService for SFAges'; 
        $sql = 'SELECT id,agerange FROM modx_sfages';
         
        $output ='<select id="age"><option>Please select...</option>';
        
        $m = $modx->getManager();
        
        $ages = $modx->getCollection('SFAge');
        if ($ages && count($ages)>0) {
          foreach ($ages as $age) {
            $ageArray = $age->toArray();
            $output .='<option value="'.stripslashes($ageArray['id']).'"';
            if ( $scriptProperties['fields']['age'] == $ageArray['id'] )
              $output .= ' selected="yes"';
            $output .= '>'.stripslashes($ageArray['agerange']).'</option>';
          }
        }
        
        $output .='</select>';
         
        return $output;