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

    I wrote a snippet to populate a <select> list (as per http://modxcms.com/forums/index.php?topic=22072.0). I built-in a way to select the chosen option based on a GET parameter:
    <?php
        while ($row = mysql_fetch_assoc($res)) {
          $out .= "<option value=\"{$row['pagetitle']}\"";
          if ($row['id'] == $_GET['job']) {
            $out .= ' selected="selected"';
          }
          $out .= ">{$row['pagetitle']}</option>\n";
        }
    ?>


    The problem is - eForm seems to pick which item is selected, and overwrites the value I’ve chosen.

    So I tried passing in the field name and a value on the URL, by-passing my code:
    http://example.com/jobs/apply.html?role=Manager


    eForm doesn’t respond to this, and still writes [tt]selected="selected"[/tt] on the first option tag, and removes the one I’ve added.

    How can I get eForm to respect my wishes - or modify my code to work with it?

    Thanks!
      MAPLEDESIGN MODX development and MODX web design and custom development. Proud to serve UK and International clients!
      • 30223
      • 1,010 Posts
      Have you checked what output your code actually produces? i.e. before eform gets to parse the template? What does that look like?
        • 22897 ☆ A M B ☆
        • 209 Posts
        Hi Toby,

        Sorry for the delay, things are a bit manic! Yes I’ve checked the code, what is produced by my snippet is:
        <option value="">Speculative enquiry</option>
        <option value="Sales Manager" selected="selected">Sales Manager 23</option>
        


        This is then assigned to the form using:
           $templates['tpl']=str_replace('[+display_job_options+]',$out,$templates['tpl']);


        When the form is displayed in the browser, the source code has changed to:
        <select name="position" >
        <option value="" selected='selected'>Speculative enquiry</option>
        <option value="Sales Manager" >Sales Manager 23</option>
        
           </select>


        It’s always the first option selected (something which my code *never* generates - if there’s not a ’real’ job to select, no option is selected by default.

        So something, somewhere is changing the option. I’d love to know if there’s a way to tell eForm which to select - I can understand why it selects one, but I need to direct it to pre-select the right item.
          MAPLEDESIGN MODX development and MODX web design and custom development. Proud to serve UK and International clients!
          • 30223
          • 1,010 Posts
          eForm should honour the selected attribute when parsing your form template. Post your templates, snippet call and code and I’ll have a look to see where it goes wrong.
            • 22897 ☆ A M B ☆
            • 209 Posts
            Quote from: TobyL at Jul 04, 2009, 12:00 AM

            eForm should honour the selected attribute when parsing your form template. Post your templates, snippet call and code and I’ll have a look to see where it goes wrong.

            Snippet calls in my template
            [!GetJobOptions!]
            [!eForm? &formid=`JobForm` &tpl=`JobForm` &report=`JobEmail` &eFormOnBeforeFormParse=`eformGetJobOptions` &to=`[email protected]` &sendAsText=`1` &subject=`Job Enquiry`!]


            GetJobOptions snippet
            <?php
            if( !function_exists('eformGetJobOptions') ) {
              function eformGetJobOptions(&$fields,&$templates){
                global $modx;
                
                $out = "<option value=\"\">Speculative enquiry</option>\n";
                
                $res = mysql_query("SELECT id,pagetitle FROM modx_site_content WHERE parent='24' AND published='1'");
                
                while ($row = mysql_fetch_assoc($res)) {
                  $out .= "<option value=\"{$row['pagetitle']}\"";
                  if ($row['id'] == $_GET['job']) {
                    $out .= ' selected="selected"';
                  }
                  $out .= ">{$row['pagetitle']} {$row['id']}</option>\n";
                }
                
                $templates['tpl']=str_replace('[+display_job_options+]',$out,$templates['tpl']);
                return true;
              }
            }
            
            return '';
            ?>


            JobForm chunk
            <div class="hr"></div>
            <script type="text/javascript" src="/js/valid.js"></script>
            [+validationmessage+]
            <form id="JobForm" action="[~[*id*]~]" method="post" enctype="multipart/form-data">
            <table style="margin-left: 20px">
              <tr>
                <td width="135"><strong>Name</strong></td>
                <td><input type="text" name="name"  eform="Your Name::1:Expected at least two words:#REGEX /^\w+\s\w+/i" onblur="return 'Name_req'"></td>
              </tr>
              <tr>
                <td>Current job title</td>
                <td><input type="text" name="jobtitle"></td>
              </tr>
              <tr>
                <td>Prospective position</td>
                <td><select name="position">
            [+display_job_options+]
               </select></td>
              </tr>
              <tr>
                <td><strong>Email</strong></td>
                <td><input type="text" name="email"  eform="Your Email address:email:1" onblur="return 'Email_req'"></td>
              </tr>
              <tr>
                <td><strong>Phone number</strong></td>
                <td><input type="text" name="telephone" eform="Your Phone number::1" onblur="return 'Phone number_req'"></td>
              </tr>
              <tr>
                <td>Covering letter</td>
                <td><textarea name="letter" style="height: 100px"></textarea></td>
              </tr>
              <tr>
                <td> </td>
                <td> </td>
              </tr>
              <tr>
                <td>Attach CV</td>
                <td><input type="file" name="cv" eform="Attachment:file:0:Only upload of text documents are supported:#LIST pdf,txt,doc,rtf,docx "></td>
              </tr>
              <tr>
                <td> </td>
              <td> </td>
              </tr>
            </table>
            <div class="hr"></div>
            <a href="[~6~]" style="float: left">« back to jobs</a>
            <input id="submit" type="image" name="submit" src="images/apply-now.png" width="216" height="46" onclick="return Validate(document.forms.TalkToUs)" style="float: right">
            </form>
            
              MAPLEDESIGN MODX development and MODX web design and custom development. Proud to serve UK and International clients!
              • 22897 ☆ A M B ☆
              • 209 Posts
              Looks like this bug was fixed between 1.4.4.5 and 1.4.4.6, as I’ve upgraded and it now works.

              By the way, your 1.4.4.6 package reports in the snippet that it’s still 1.4.4.5, which is confusing!

              Regards,
              Peter
                MAPLEDESIGN MODX development and MODX web design and custom development. Proud to serve UK and International clients!