We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16732
    • 592 Posts
    Thanks sheeley for your answer.

    Tobyl is there a hack or what else to do what I want :

    I grabs the infos from a database table with an eFormOnBeforeFormMerge function to populate my form.
    It works with input fiels but, how I can do for my select options ?

    Many thanks for your answer and help

    I use the eForm version from 0961 on a 095 installation.
      • 16732
      • 592 Posts
      No one can help me to solve my problem undecided... I don’t ask a lot of help on this forum but when I do, I never get any answer ... to bad
        • 30223
        • 1,010 Posts
        Sorry both, I completely missed this topic for a while...

        @sheeley I’ve just tried making a form with the form template you posted. Works fine for me. What is exactly your problem? Are you trying to set the selected option programatically or does the form not work at all?

        @laurentc eFormOnBeforeFormMerge is not the right place to populate a form dynamically. Try using the eFormOnBeforeParse event instead. Have a look at the wiki article about dynamically populating forms. This doesn’t describe how to populate select fields but the principal is the same.

        To populate a select you need to set one place holder for all option statements within each select. Something like this:

        <select name="state">
        [+state_options+]
        </select>
        


        Then you need a function that is called in the eFormOnBeforeParse event which replaces this place holder:
        <?php
        function fillSelectOptions(&$fields, &$templates){
          //yes I live in Australia :) ofcourse this can just as easily come from a DB
          $options = array('VIC', 'NSW', 'TAS', 'NT,'ACT','WA','QLD','SA');
          
          foreach($options as $option)
            $option_tags = '<option value="'.$option.'">'.$option.'</option>';
        
          //$templates['tpl'] contains your form template
          $templates['tpl'] = str_replace('[+state_options+]',$option\_tags,$templates['tpl']);
           
          return true;
        }
        ?>
        


        This is a rather simple function but you can easily expand it to include database calls and some error checking or handling of post variables. That’s left up to you. Because this is called before eForm parses the form all values will be available to process and validate against. Good luck.
          • 30056
          • 7 Posts
          I thought that this thread was looking at redrawing for example an update form, with all the fields prefilled, as per the recorded data, stored in a db maybe. I am trying to do this with a number of selct fields on one form.

          [+state+] would already contain the option previously selected eg ’NSW’

          I would have expected some how to have

          if($option=[+state+]) then
          $option_tags = ’<option value="’.$option.’" selected="yes">’.$option.’</option>’;
          else
          $option_tags = ’<option value="’.$option.’">’.$option.’</option>’;
          endif

          Not sure what the syntax would be or if this is possible. Can anyone correct this.
            • 30223
            • 1,010 Posts
            Quote from: timst at Jul 17, 2008, 03:54 AM

            I thought that this thread was looking at redrawing for example an update form, with all the fields prefilled, as per the recorded data, stored in a db maybe. I am trying to do this with a number of selct fields on one form.

            [+state+] would already contain the option previously selected eg ’NSW’

            It is not. This thread and this sub-forum is about eForm, a very flexible form snippet. Learn more about it by following the links in my signature.

            If you have already (looked at eForm that is) and you want to build a form which gets it’s data from a database have a look at eform2db. Although that’s more for getting things into a database it’ll give you some pointers to start from.

            Any more questions fellow countryman? smiley
              • 30056
              • 7 Posts
              Thank TobyL for pointing that out. laugh

              Great start for my first post! By thread I should have said Topic!

              I have been reading all of the eForm topics and trying to implement something on my website that I used to used Mambo and FacileForms, but I have decided to redesign using MODx. I have followed the Eform2db topics and have managed to implement a large application form which the user fills and I then get an email and the data is sent to the DB.

              The next part is for me to implement a method for updating data they have entered. I have got this to partially work as you say there is an example in the wiki showing db2eForm, but as you say this works for <input> entries, however I then modified in that example name to be a <select> with <options>. In that example I can read the data from the database [+name+] first example is ’tim’.

              However as other people in this topic have stated they are struggling to get the <option> with [+name+] in it to be preselected when the form is drawn.

              The desired html syntax would be:

              <option value="sue" >sottwell</option>
              <option value="tim" selected="yes" >timst</option>
              <option value="toby" >TobyL</option>

              I have been trying to implement something with a snippet call, but having not progress much at present other than grabbing other peoples examples and modifying them for my own means, including many from yourself TobyL. I can not suss this out. I believe this is also what laurentc and goddang were also trying to do.

              In your first reply TobyL, you use [+name:tim+] style of notation, what should this return, because it appears to be empty.

              I tried creating a snippet CompareOption as a function that takes to params, if the two params are equal then it would:

              echo ’selected="yes"’

              however, I don’t think I can place the snippet in the eFormMailMe chunk as I am currently:

              <option value="sue" [!CompareOptions? &param1=`sue` &param2=`[+name+]`!] >sue</option>
              <option value="tim" [!CompareOptions? &param1=`tim` &param2=`[+name+]`!] >tim</option>
              <option value="joe" [!CompareOptions? &param1=`joe` &param2=`[+name+]`!] >joe</option>

              Can someone give me pointers as to how this can be done?

              I do hope that all this makes sense...

              Thanks Tim.

                • 30223
                • 1,010 Posts
                eForm gets all it’s data from the form template it parses. It doesn’t do much when the form is displayed the first time, i.e. before teh form is posted. So the trick to pre-filling a form is that you need to do it before eForm parses the form. For example you may have a template like below:
                <form id="testform" action="[~[*id*]~]" method="post">
                <label>Name</label><input type="text" name="name" value="[+name_value+]" eform="Your Name::1" /><br />
                <label>Gender</label><select name="gender">
                <option value="male" [+selected_male+]>Male</option>
                <option value="female" [+selected_female+]>Female</option>
                <option value="undisclosed" [+selected_undisclosed+]>None of your business</option>
                </select>
                <input type="submit" value="Submit" />
                </form>
                


                As you can see there are several placeholders in the form template which you aim to fill from a database or such. These placeholders have nothing to do with eForm itself. This is a constant source for confusion. Although eForm in some specific cases needs placeholders (such as with CAPTCHA) in general eForm generates it’s own placeholders when it parses the form. Notice especially the option placeholders You mentioned my post in which they were [+field:value+] and the ones I’ve used here are not in that format. The field:value variety is a format eForm uses internally but as we’re going to replace these placeholders before eForm sees them there’s no reason to stick to that.

                So why these placeholders? They are there for you to fill in before eForm get’s to parse the template. This you can do by writing a function that is called by eForm at the eFormOnBeforeFormParse event. Here’s an example:
                <?php
                if(!function_exists('defaultFillForm')){}
                  function defaultFillForm( &$templates, &$fields){
                    //get logged in username
                    $username = $_SESSION['webShortname']; 
                    //arbitrary function to retrieve user data from some DB
                    $record = getRecordFromDB($username); 
                    
                    $name = $record['fullName']; //get value
                    $gender_ph = '[+selected_'.$record['gender'].'+]'; //get placeholder tag from value
                    //replace placeholders in form template
                    $templates['tpl'] = str_replace(
                      array('[+name+]',$gender_ph),
                      array($name,'selected="selected"'),
                      $templates['tpl']
                    );
                    return true; //important!
                  }
                }
                ?>
                

                Just note that the getRecordFromDB function does not exist! The code is just to explain something and you’ll need to write your own event code.
                Assuming that the user’s name is Josh Johanson and he didn’t disclose his gender the form template now looks like this:
                <form id="testform" action="[~[*id*]~]" method="post">
                <label>Name</label><input type="text" name="name" value="Josh Johanson" eform="Your Name::1" /><br />
                <label>Gender</label><select name="gender">
                <option value="male" [+selected_female+]>Male</option>
                <option value="female" [+selected_female+]>Female</option>
                <option value="undisclosed" selected="selected">None of your business</option>
                </select>
                <input type="submit" value="Submit" />
                </form>
                


                As you see the values are filled in but there’s still a few placeholders left over. You could clean these up in the function but in this case they’ll get cleaned up by eForm when it’s done. Now eForm can do it’s work and should display the form with the default values.

                Just to show you this is what the form will look like after eForm has parsed it and before eForm displays it. As you can see there’s plenty of placeholders but these are used internally by eForm and you mostly don’t need to worry about them.
                <form id="test1" action="index.php?id=51" method="post">
                <label>Name</label><input type="text" name="name" class="[+name_class+]" value="[+name+]" /><br />
                <label>Gender</label><select name="gender" class="[+gender_class+]">
                <option value="male" [+gender:male+]>Male</option>
                <option value="female" [+gender:female+]>Female</option>
                <option value="undisclosed" [+gender:undisclosed+]>None of your business</option>
                </select>
                <input type="submit" class="[+_class+]" value="Submit" />
                <input type="hidden" name="formid" class="[+formid_class+]" value="test1" /></form> 
                

                  • 3632
                  • 22 Posts
                  TobyL: Removed double posting: see this separate topic