We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17851
    • 213 Posts
    I have an online job application form with a dropdown that specifies the position being applied for. Based on the ’position’ selected, it will goto the email address responsible for that job. I see how to use this in conjunction with ’mailselector’ parameter, but I need to use the text in the dropdown for my email subject. Apparently, ’mailselector’ only works if the dropdown values are integers which specify the index of the ’to’ parameter.

    Is there a way around this? To be able to use the dropdown text but also be able to use ’mailselector’? If not, can I use eFormOnBeforeMailSent to dynamically set the target email address (the ’to’ parameter) based on the ’position’ field?
      Mark Macatee
      President
      Power 10 Solutions
      http://www.power10solutions.com
      • 17851
      • 213 Posts
      anyone come across this scenario before? using dropdown for both the "mailselector" parameter AND as a text value?
        Mark Macatee
        President
        Power 10 Solutions
        http://www.power10solutions.com
        • 16278
        • 928 Posts
        I set up a feedback form with a similar requirement using events. You can probably simplify it - I wanted to the client to be able to update the addresses from a TV without editing chunks or files.

        The components of my solution are the form, the eForm call, two eForm extender functions (plus one for validation), a chunk and the TV.

        The subject field is called cSubject, and this part of the HTML is inserted using a placeholder called [+rhMessageList+]

        The eForm call looks like this:
        [!eFormExtenders?!]
        [!eForm? &formid=`contactForm` &tpl=`pkMessageForm` &eFormOnValidate=`valext` 
        &subject=`((cSubject))` &report=`pkResponseMessage` 
        &eFormOnBeforeMailSent=`sendMessage` &eFormOnBeforeFormMerge=`setMessages` 
        &sendirect=`1`!]

        (Validation includes a custom check that the sender has included either a phone number or an email address. I think the &sendirect parameter is required for the method described here.)

        The extender functions:
        <?php
        /* extensions to eForm for Contacts page emails to different addresses */
        
        function setMessages(&$fields) {
        /*  set up options for dropdown list held in chunk rhMessageList
        	save messages in placeholder for use by sendMessage */
        	global $modx;
        
        	$options = explode('||',$modx->getChunk('rhMessageList'));
        
        	$select  = "\t" . '<span class="edLabel"> </span>';
        	$select .= "\t" . '<span class="edField">';
        	$select .= "\t" . '<select name="messageType" id="messageType" size="1">' . "\n";
        
        	foreach ($options as $option) {
        		$item = explode('==', $option);
        		$select .= "\t" . '<option value="' . $item[1] . '"';
        		$select .= ($item[1] == 0) ? 'selected="selected"' : NULL;
        		$select .= '>' . $item[0] . "</option>\n";
        		}
        	$select .= "\t</select>\n\t</span>\n";
        
        	$fields['rhMessageList'] = $select;
        }
        
        function valext(&$fields,&$vMsg,&$rMsg) {
        /* Require either phone number or email or both for contact form */
        	if (empty($fields['cMail']) AND empty($fields['cPhone'])) {
        		$rMsg[] = '<br />An email address or phone number';
        	}
        /* Check something is selected from drop-down list, */
        	if ($fields[recipient] == '0') {
        		$rMsg[] = '<br />Please select a message subject from the list';
        	}
        	return true;
        }
        
        function sendMessage(&$fields) {
        // set up email adress and response form fields when form submitted
        	global $modx;
        
        // recreate messages for use as subject field
        	$options = explode('||',$modx->getChunk('rhMessageList'));
        	$option = explode('==',$options[$fields['messageType']]);
        	$fields['cSubject'] = trim($option[0]);
        
        // Match email address, stored as TV rhMailAddresses in format
        // <1>mailaddress1||<2>mailaddress2|| etc., (with returns to be stripped)
        	$mailAddresses = $modx->getTemplateVar('rhMailAddresses','id');
        	$mailAddresses = explode('||',$mailAddresses['value']);
        	$recipientAddress = trim($mailAddresses[--$fields['messageType']]);
        	$recipientAddress = ereg_replace('<.*>','',$recipientAddress);
        	$fields['email'] = $recipientAddress;
        
        /* set up strings for empty fields */
        if (empty($fields['cPhone'])) {
        	$fields['cPhone'] = '(no phone number supplied)';
        	}
        
        if (empty($fields['cMail'])) {
        	$fields['cMail'] = '(no email supplied)';
        	}
        
        	$fields['cText'] = str_replace("\n\n","\n",$fields['cText']);
        	$fields['cText'] = str_replace("\n","<br />\n",$fields['cText']);
        }
        
        ?>
         


        The rhMessageList chunk:
        - Please select message subject -==0||Request for details==1||Message to X ==2||Message to Y ==3||Comment on the website==4 


        rhMailAddresses is simply a textarea TV, with content in the format detailed in the extender function:

        Hope this meets your needs
        smiley KP
          • 17851
          • 213 Posts
          Wow, very detailed, cool solution.

          I was actually able to solve this yesterday by creating a hidden field that will store the "selectedIndex" of the dropdown. So, here’s my form:

          <form method="post" action="[~[*id*]~]" onsubmit="return onFormSubmission(this);">
          <input type="hidden" name="formid" value="fContact" />
          <input type="hidden" name="jobselindex" eform="::0::" />
          <table style="border-spacing: 0pt; border: 0pt none; width: 100%;" border="0">
          <tbody>
          <tr>
          <th>First Name <span class="req">*</span></th>
          <td><input type="text" name="fname" eform="First Name::1"/></td>
          </tr>
          <tr>
          <th>Last Name <span class="req">*</span></th>
          <td><input type="text" name="lname" eform="Last Name::1"/></td>
          </tr>
          <tr>
          <th>Email <span class="req">*</span></th>
          <td><input type="text" name="email" eform="Email:email:1"/></td>
          </tr>
          <tr>
          <th>Position <span class="req">*</span></th>
          <td>
          <select name="position" eform="Position">
          <option value="">Select One</option>
          <option value="Customer Support Engineer">Customer Support Engineer</option>
          <option value="Consultant">Consultant</option>
          </select>
          </td>
          </tr>
          <tr><td><b>Veriword</b><br/>Please enter characters<br/>in the space provided</td>
          <td><img src="[+verimageurl+]" alt="verification code" border="1"/><br/><input type="text" name="vericode" /></td></tr>
          <tr><td colspan="2"><input type="submit" name="submit" value="Submit"/> <input type="reset" name="reset" value="Clear Form"/></td></tr>
          </tbody>
          </table>
          </form>
          


          Here’s the eForm call:

          [!eForm?
             &formid=`fContact`
             &tpl=`JobApplication`
             &report=`JobAppNotify`
             &to=`[email protected],[email protected]`
             &mailselector=`jobselindex`
             &subject=`Job Application for [+position+]`
             &vericode=`1`
             &jScript=`/assets/js/forms.js`
          !]
          


          Upon submission, the javascript function onFormSubmission sets the value of hidden field jobselindex to be the selectedIndex of the dropdown. This is then used to select the email address per the eForm parameter "mailselector". Note the "eform" attribute of the hidden field, which is required to avoid the errors associated with hidden fields.

          Hope this helps someone else!
            Mark Macatee
            President
            Power 10 Solutions
            http://www.power10solutions.com
            • 17851
            • 213 Posts
            I actually came up with a better solution that’s similar to kp2’s. To avoid having to hard code the "to" field every time there’s a new job posting, I’ve assigned a TV to each job posting doc, called "PositionEmail", that stores the email of the person responsible for that job posting. In the form, I use a field called "appemail" to save the applicant’s email. (If you use "email" for the field’s name, it wants to use that for Send Direct, so we’ll distinguish the two). Also, since the posting form can be reached via a link on the actual job description page, I’m preselecting the dropdown if user came from that page.

            The form:
            <form method="post" action="[~[*id*]~]">
            <input type="hidden" name="formid" value="fContact" />
            <table style="border-spacing: 0pt; border: 0pt none; width: 100%;" border="0">
            <tbody>
            <tr>
            <th>First Name <span class="req">*</span></th>
            <td><input type="text" name="fname" eform="First Name::1"/></td>
            </tr>
            <tr>
            <th>Last Name <span class="req">*</span></th>
            <td><input type="text" name="lname" eform="Last Name::1"/></td>
            </tr>
            <tr>
            <th>Email <span class="req">*</span></th>
            <td><input type="text" name="appemail" eform="Email:email:1"/></td>
            </tr>
            <tr>
            <th>Position <span class="req">*</span></th>
            <td>
            <select name="position" eform="Position">
            <option value="">Select One</option>
            [+job_options+]
            </select>
            </td>
            </tr>
            <tr><td><b>Veriword</b><br/>Please enter characters<br/>in the space provided</td>
            <td><img src="[+verimageurl+]" alt="verification code" border="1"/><br/><input type="text" name="vericode" /></td></tr>
            <tr><td colspan="2"><input type="submit" name="submit" value="Submit"/> <input type="reset" name="reset" value="Clear Form"/></td></tr>
            </tbody>
            </table>
            </form>
            

            The functions defined in snippet "eFormFunctions":
            function getJobList(&$fields,&$templates){
            	global $modx;
            	$ref = preg_replace('/http.*\/(.*)\.html/', "$1",$_SERVER['HTTP_REFERER']);
            
            	$result = $modx->db->select('pagetitle,alias',$modx->getFullTableName('site_content'),'published and parent=79 and id<>85','menuindex');
            	//$rows = $modx->db->makeArray( $result );
            	while( $row = $modx->db->getRow( $result ) ) {
            		$sel = ($ref == $row['alias'] ? 'selected="selected"' : '');
            		$options .= "<option value=\"".$row['alias']."\" $sel>".$row['pagetitle']."</option>\n";
            	}
            	$templates['tpl']=str_replace('[+job_options+]',$options,$templates['tpl']);
            	return true;
            }
            function efBeforeSend(&$fields) {
            	global $modx;
            	$email = '';
            
            	// Get email for the position specified by the alias (selected in the dropdown)
            	$alias = $fields['position'];
            	$res = $modx->db->select('id,pagetitle',$modx->getFullTableName('site_content'),"alias='$alias'");
            	//if ($modx->db->getRecordCount($res)) {
            	if ($row = $modx->db->getRow( $res )) {
            		$posid = $row['id'];
            		$fields['position'] = $row['pagetitle'];
            		$tv = $modx->getTemplateVarOutput(array("PositionEmail"), $posid);
            		if ($tv['PositionEmail'] != '') $fields['email'] = $tv['PositionEmail'];
            	}
            	return true;
            }
            

            And the snippet call itself:
            [!eFormFunctions!]
            [!eForm? &formid=`fContact` &tpl=`JobApplication` &report=`JobAppNotify` &sendirect=`1` &subject=`Job Application for [+position+]` &vericode=`1` &eFormOnBeforeFormParse=`getJobList` &eFormOnBeforeMailSent=`efBeforeSend` !]
            


            "to" parameter is not specified b/c if "PositionEmail" is blank it will send to the MODx admin. The main thing is that the OPTION value is the document’s "alias" so I can preselect a value if the form was reached via a link from the job posting page itself.
              Mark Macatee
              President
              Power 10 Solutions
              http://www.power10solutions.com