We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5689
    • 289 Posts
    I needed a way to add a dynamically generated select input field in my eForm for choosing who to send the email to. I also needed a way to generate the list of emails to put in my eForm call’s ’to’ paramater. I decided to just create a document for each email address as a child of my Contact page. I set up two TVs. One is Email (the address) and one is JobPosition (it’s for a church, so this would contain things like ’Senior Pastor’). I am simply using the page title as the name for the person the email address belongs to. I created a snippet (called Testt for now) that sets up a function that reads all of these documents and generates the <option>s for the input in the HTML form. This same snippet also sets a placeholder called eFormRecipientEmailAddresses that I then use in my eForm call for the &to parameter. Without the &eFormOnBeforeFormParse parameter set, the form shows up like normal (except the select input contains no options, of course). But when it is set like I need, the error message for the form is set to a slash, and so are the values for some of my inputs. I have absolutely no idea where the slashes are coming from, but I assume I have some sort of error in my code.

    Here is the content of my Contact Us page:
    [!Testt!]
    
    [!eForm? &formid=`contact` &from=`[+email+]` &fromname=`[+name+]` &subject=`[+subject+]` &to=`[+eFormRecipientEmailAddresses+]` &mailselector=`to` &tpl=`Contact` &report=`ContactEmail` &gotoid=`[*id*]` &vericode=`1` &thankyou=`ContactThanks` &eFormOnBeforeFormParse=`eFormGetRecipientOptions` !]


    Here is the code for the snippet:
    <?php
    if(!function_exists('eFormGetRecipientOptions')) {
    		function eFormGetRecipientOptions(&$fields, &$templates) {
    			global $modx;
    			$documents = $modx->getDocumentChildren($id=12, $active=1, $deleted='0', $fields='id,pagetitle');
    			$options = '';
    			$count = 0;
    			foreach($documents as &$thisDocument) {
    				$count++;
    				$templateVariables = $modx->getTemplateVarOutput(array('JobPosition'), $thisDocument['id'], 1);
    				$options .= '<option value="' . $count . '">' . $thisDocument['pagetitle'] . (isset($templateVariables['JobPosition']) && $templateVariables['JobPosition'] ? ' (' . $templateVariables['JobPosition'] . ')' : '') . '</option>';			
    			}
    			$templates['tpl'] = str_replace('[+eFormRecipientOptions+]', $options, $templates['tpl']);			
    			return true;
    		}
    	}
    	
    	$documents = $modx->getDocumentChildren($id=12, $active=1, $deleted='0', $fields='id,pagetitle');
    	$emailAddresses = array();
    	$count = 0;
    	foreach($documents as &$thisDocument) {
    		$count++;
    		$templateVariables = $modx->getTemplateVarOutput(array('Email'), $thisDocument['id'], 1);
    		$emailAddresses[] = $templateVariables['Email'];
    	}
    	
    	$modx->setPlaceholder('eFormRecipientEmailAddresses', implode(',', $emailAddresses));
    ?>


    And here is the chunk for the form:
    <p id="error">[+validationmessage+]</p> 
    <form method="post" action="[~[*id*]~]" id="contact" class="clear"> 
    	<p><input type="hidden" name="site_name" value="[(site_name)]" eform="Site Name::1::#REGEX /[(site_name)]/" /></p>
    	<p><input name="formid" type="hidden" value="contact" /></p>
    	<label for="cfName">Your Name
    		<p><input name="name" id="cfName" class="text" type="text" eform="Your Name::1:" /></p>
    	</label> 
    	<label for="cfEmail">Your Email Address
    		<p><input name="email" id="cfEmail" class="text" type="text" eform="Email Address:email:1" /></p>
    	</label>
    	<label for="cfEmail">Recipient
    		<p><select name="to" id="cfTo">
    			[+eFormRecipientOptions+]
    		</select></p>
    	</label>
    	<label for="cfRegarding">Subject
    		<p><input name="subject" id="cfRegarding" class="text" type="text" eform="Form Subject::1" /></p>
    	</label> 
    	<label for="cfMessage">Message
    		<p><textarea name="message" id="cfMessage" eform="Message:textarea:1" rows="10"></textarea></p>
    	</label>
    	
    	<p class="instructions">Please type the verification code seen in the image.</p>
    	<p><img src="[+verimageurl+]" border="1" alt="Verification Code" id="cfVericodeImage" /></p>
    	<label for="vericode">Verification Code
    		<p><input name="vericode" id="cfVericode" size="20" type="text" /></p>
    	</label>
    	<label> </label>
    	<p><input type="submit" name="contact" id="cfContact" class="button" value="Send Email" /></p> 
    </form>


    I’m wondering if anybody notices any problems that may be causing this strange outcome with the slashes.

    If seeing the actual webpage is necessary, I’ll upload it, but right now I’m working on my local server. If I need to upload it, just let me know.

    Thanks!
      I&#39;m learning more about MODx all the time and loving it.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      It appears to be some kind of error condition: if you change your getDocumentChildren calls to this
      $documents = $modx->getDocumentChildren(12, 1, 0, 'id,pagetitle');

      it doesn’t do it. Whether it actually works or not I don’t know.
        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
        • 5689
        • 289 Posts
        Thanks! Apparently I forget to change those when I copied and pasted some of the code from somewhere else. I copied off the specification page for the getDocumentChildren function where the parameter names were shown, and I forgot to change it to just the values.

        It’s not working correctly just yet, but at least the slashes are gone and my form shows up properly again. laugh
          I&#39;m learning more about MODx all the time and loving it.