We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29181
    • 480 Posts
    Thanks for following up there TobyL.

    That mail_id was in fact somewhere in the wiki link so I thought it had something to do with checking if the form had been built. After that small amendment I can get it to display the selected number from the url, or if no selected number is concatenated to the URL it will output the default content.

    I’ll look at outputing the option values from an array now, which no doubt the code you initially posted will help me with no end .

    This is going to open up no end of possibilities in other parts of ModX, meaning anything I can do in PHP (which isn’t all that much really wink) will be possible in ModX with very little extra work.

    Once again thanks for your help...and a great snippet!!!

    Taff

      Adrian Lawley: www.adrianlawley.com
      • 29181
      • 480 Posts
      Thanks for your help once again, it all works as I hoped it would TobyL...

      For anyone else looking for a solution, this was the code for the function which is contained within a snippet called [!SelectBoxParser!]

      <?php
      function populate(&$fields, &$templates){
      $names = array(
      	"General Information" => 1,
              "Something more personal" => 2,
              "Something very personal" => 3,
              "Send spam here" => 4,
              "Any other gumpf" => 5,
      );
      global $modx; // Access to DocumentParser
      $evt =& $modx->event; // The Snippet call event (not eFormOnBeforeFormParse event) 
      // Replicate $isPostBack variable
      $validFormId = ($evt->params['formid'] == $_POST['formid']);
      $isPostBack = ($validFormId && count($_POST) > 0); 
      // (bool)true if the form is being updated
      $mail_id=trim($_REQUEST['sel']);
      //save the value of selected to a variable
      if (!$isPostBack) {
      // Check for NOT postback, cuz we want to populate an empty form
      $chosen = isset($_REQUEST['sel'])?$_REQUEST['sel']:99;
      //look if the URL has a &sel= and a number otherwise give it a number not used
      $optionTags ="<select name=\"recipient\" id=\"cfRecipient\" eform=\"Form Subject::1\"><option value=\"23\">Please Select</option>";
      //This is the start of the select tag
      
      foreach ($names as $name => $value){
      	$selected=($value == $chosen)? ' selected' : '';
      	$optionTags .= "<option value = \"".$value."\"".$selected.">".$name."</option>\n";
      }
      //loop thru the array looking for a value equal to sel, if you find one add selected in the option tag
      $optionTags.="</select>";
      //close the select tag
      
      /* This was for testing purposes by using a [+debug+] placelholder somewhere within the template
      if ($mail_id > 0){
             $templates['tpl'] = str_replace('[+debug+]','Using '.$mail_id.' as the selected option value',$templates['tpl']);
      }else{
      	$templates['tpl'] = str_replace('[+debug+]','No email specified',$templates['tpl']);
      }
      */
      $templates['tpl'] = str_replace('[+SelectBox+]',$optionTags,$templates['tpl']);
      //Replace our placeholder [+SelectBox+] with the contents of the $optionTags variable
      } else {
      // The form has beed posted, let's not disturb the data.
      }
      return true; // A value of false will stop the eForm parser
      }
      ?>
      


      Other things we need to change are the page where we are displaying the form so it looks like this (or similar). Note the additional &eFormOnBeforeFormParse=`populate` parameter in our eform call and that we are calling [!SelectBoxParser!] which is the snippet containing our populate function. If this bit isn’t working correctly you will get a message saying that it can’t find said function (at least with FF)

      [!SelectBoxParser!]
      [!eForm? &formid=`ContactForm` &subject=`FEEDBACK` &to=`[email protected],[email protected],[email protected],[email protected],[email protected]` &mailselector=`recipient` &ccsender=`1` &tpl=`ContactForm` &report=`ContactFormReport` &gotoid=`46` &eFormOnBeforeFormParse=`populate` !]


      Note the emails in the correct order to square up with the vaules in the array at the top and that &eFormOnBeforeFormParse is calling our function "populate"

      The only other thing you need to do is add the [+SelectBox+] placeholder to your ContactForm tag.

      Hope this helps someone as much as TobyL helped me.

      Taff
        Adrian Lawley: www.adrianlawley.com
        • 17459
        • 268 Posts
        I tried to test this script: copy-paste your code to populate snippet, add &eFormOnBeforeFormParse=`populate` but get error on my page:

        Fatal error: Call to undefined function populate() in C:\HostingSpaces\entropia\pickup.entropia.us\wwwroot\assets\snippets\eform\eform.inc.php on line 138

        http://pickup.entropia.us/phpinfo.php
          • 29181
          • 480 Posts
          Hi,
          the snippet that you have copied and pasted the populate function into needs to be called before the eform call, otherwise eform won’t be able to find the function and you will get that error. I created one called SelectBoxParser as I mentioned on the second line of my previous post.

          Hope that helps,
          Taff
            Adrian Lawley: www.adrianlawley.com
            • 17459
            • 268 Posts
            Thanks smiley ok now smiley

            Thanks for good example!

            P.S. Add plz <?php and ?> to your post (Posted on: 17 June 2007, 13:14:11)