We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22244
    • 144 Posts
    How in the world would I be able to make the form become dynamic so that I can send data to different database tables depending in a selected value from a visitor.

    I have a formit form where I set a value with a dropdown.
    In my snippet "mycomp" that I hook in the formit call I check for this value.

    When the dropdown value is alfa then I want to set some property values (from formit2db) in the formit call.

    [[!FormIt?
    &hooks=`mycomp,formit2db`
    &packagename=`[[+pack]]`
    &classname=`[[+class]]`
    ]]

    I tried this in my snippet but the placeholders stay empty.
    <?php
    if ($hook->getValue('comp_code') == 'alfa' ){
    $data = array(
        'pack' => 'Appone',
        'class' => 'ApplicationOne',
    );
    return $data;
    }
    


    I have tried many options like runsnippet etc etc but the placeholders always stay empty.
    If anyone has any ideas please let me know because I'm stuck after 8 hours of trial and ERROR.
      • 34926
      • 87 Posts
      I don't quite understand what you are trying to do but if you want to set FormIt placeholders in a hook you would use setValues.

      E.g (code not tested).
      if ($hook->getValue('comp_code') == 'alfa' ) {
      	$hook->setValues(array(
      		'pack' => 'Appone',
      		'class' => 'ApplicationOne',
      	));
      }
      
      return TRUE;
      


      Hooks should only return TRUE or FALSE IIRC. Also FormIt by default uses the fi. prefix
      so your placeholders might be [[+fi.pack]] etc.

      Personally I would approach this differently and use Javascript instead, possibly with
      AJAX.

      From what I gather, with your approach of multiple submits(?), you will need
      to use FormItRetriever.
        • 4172
        • 5,888 Posts
        you can do it this way:

        
        <?php
        
        $comp_code = $modx->getOption('comp_code',$_REQUEST,'');
        
        if (!empty($comp_code)){
            
            switch($comp_code){
                case 'alfa':
                    $scriptProperties['packagename'] = 'Appone';
                    $scriptProperties['classname'] = 'ApplicationOne';
                break;
                //other cases go here
            }
        }
        
        $modx->runSnippet('Formit',$scriptProperties);
        
        


        and call it:

        [[!myFormItWrapper?
        &hooks=`formit2db`
        &otherProperties
        ]]



          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 22244
          • 144 Posts
          Hi All,

          Thank you for replying. I managed to get it all working. The issue was that the formitretriever was placed too low on the page.
          So now I can write a submitted form to it's own database table based on a selection that somebody made in the form.

          I can also retrieve it with rowboat and make a PDF from the dynamically filled page with "client-entry-data".

          The last thing I'm stuck on is that the names of the two attached images are written to the database as "array".
          But when I try to work in a custom snippet with the placeholder array for one of the attachments I get:
          Warning: array_shift() expects parameter 1 to be array, string given
          When I print this string I get: 2_photo.jpg,image/jpeg,/tmp/phpCHukqj,0,79537

          Is there a way I can retrieve only the name of these images so they get written to the database instead of "array"?

          (I'm using formit2file and formit2db with formit.)
            • 22244
            • 144 Posts
            I see that the formit2db snippet has a json encoded arrayFields property but how does it work? Is this the solution? Fill in the two attachment placeholders and the name will be stored?