We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16382
    • 36 Posts
    Well I spent the better part of a day creating this humble snippet (yes, my kung fu is very weak)

    global $table_prefix;
    
    $sql = "SELECT * FROM ".$table_prefix."site_tmplvar_contentvalues WHERE tmplvarid = 11";
    $rs = $modx->db->query($sql);
    while ($row = mysql_fetch_array($rs)) {
            echo "<option value=\"".$row['value']."\">";
            echo $row['value'];
            echo "</option>";
    }


    Only to later realize e-form doesn’t allow for such things, it talks about only allowing functions that return a true/false value... Okay, not being quite sure what this means, is there some way I can dynamically create a drop-down menu in e-form, or is there some way I can make my code usable in e-form.

    Any help is, as always, greatly appreciated smiley
      • 30223
      • 1,010 Posts
      Have a look at this article in the wiki: http://wiki.modxcms.com/index.php/Populate_eform_with_dynamic_data

      If you understand that you can easily adapt it to your needs...
        • 20508
        • 157 Posts
        You can create a selection of drop-down links using the following code in your chunk that contains the contact form itself:

        <p><label accesskey="s" for="department">Subject</label>

        <select name="department">
        <option value="1">Request for brochure and postcard sample</option>
        <option value="2">Inquire about an order</option>
        <option value="3">Request a custom quote</option>
        <option value="3">Change your address</option>
        <option value="3">Send us feedback</option>
        </select></p>

        Hope that helps and or is part of what your after.

        Cheers,
        Jess
          • 16382
          • 36 Posts
          Thanks for the reply Jelle (aka TobyL),

          Now I may be wrong but it seems to me this function just returns one value, or one row from the db. I am looking to return an array and then to iterate over the array to create a dynamically generated drop-down list. This way every time the client enters a new model number into the database it automatically gets included into the submission form.

          Maybe I need to learn more about functions but correct me if I am wrong...
            • 30223
            • 1,010 Posts
            It’s not what the eForm event functions return that is important here. It’s what you do with the function parameters. The parameters are referenced meaning that if you change their value within the (event) function you are actually changing the value of the original variable. (See http://au.php.net/manual/en/language.references.php)

            When you create a function for the eFormObeforeFormParse event in the function declaration you place an ampersand before both parameters.
            //<?php
            function beforeFormParseFunction( &$fields, &$templates){ //or was it ( &$templates, &$fields)? you better check that!
                //your code
            }
            //?>
            

            In your case you want to make changes to the form template by adding options to a select field. The form template is stored in the function parameter $templates[’tpl’]. By setting a placeholder in your form template and replacing this with your db generated options in the $temapltes[’tpl’] variable you are actually changing the template outside the function.

            //<?php
            function beforeFormParseFunction( &$fields, &$templates){
              global $modx;
            
              //get options from database    
              $sql = "SELECT * FROM ".$modx->getFullTableName("site_tmplvar_contentvalues"). " WHERE tmplvarid = 11";
              $rs = $modx->db->query($sql);
              while( $row = $modx->getRow($rs) ) {
                    $options .= "\t<option value=\"".$row['value']."\">" . $row['value'] . "</option>\n"
              }
              //insert into template... assuming thet you have a [+dynamicOptions+] plcae holder :)
              $templates['tpl'] = str_replace('[+dynamicOptions+]',$options,$templates['tpl']);
              
              //all done,.. get out of here
              return true;
            //?>
            


            That’s pretty much it. You should of course extend this with a bit of error checking and such. I’d create the whole select field from the function and first check if the query actually returned anything. If not don’t bother about a select field at all (or instead place a hidden field with a default value).

            Anyway, have a read through the documentation and with this you should be getting along fine now.
              • 16382
              • 36 Posts
              Thanks TobyL!

              The query in your code was missing a semicolon( ; ) at the end of it, but otherwise, it does exactly what I was looking for. Thanks for holding my hand through the process... Reading about functions and methods and what not is not too helpful until you try and actually create one by yourself. It is through this process that one learns best what these thing can do and how they can help you. You have given me a big push in the right direction. So thanks, again.