We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33372
    • 1,611 Posts
    Hmm...

    Well something definitely seems to be broken in function formMerge. I cannot get it to add selected or checked to listbox, checknox, or radio buttons. I discovered a couple of minor errors in that function, but I haven’t been able to understand the logic of it well enough to fix it.

    On my forms, the [+fieldname:fieldvalue+] items are replaced with blanks, so they are being parsed (just not correctly). Damned if I can figure out where, though. It seems as if there is some confusion as to whether $lastitems is an array or not...?
      "Things are not what they appear to be; nor are they otherwise." - Buddha

      "Well, gee, Buddha - that wasn't very helpful..." - ZAP

      Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
      • 30223
      • 1,010 Posts
      On my forms, the [+fieldname:fieldvalue+] items are replaced with blanks, so they are being parsed (just not correctly). Damned if I can figure out where, though. It seems as if there is some confusion as to whether $lastitems is an array or not...?

      The problem with radio and check boxes stems from formMerge() never finding a match for $name in the array $formats. In formMerge The preg_match_all call on line 287 returns an array with "name:value" for checkboxes and the like but the formats declared in the snippet call only store the "name" as a key.

      One solution is to split the name and value before testing the formats array on line 285. See the comments in the code below. For me this works. The form now ’remembers’ checkboxes and radios.

      Example form chunck:
      <h3>Order your lunch online</h3>
      <form method="post">
      <input name="formid" type="hidden" value="testForm" />
      <p><span style='color:red'>[+validationmessage+]</span></p>
      <p>
      	Your Order: <input type="text" name="yourOrder" id="yourOrder" value="[+yourOrder+]" />
      </p>
      <p>
      	<strong>Extras:</strong><br />
      	<input type="checkbox" name="withSauce" value="Yes"[+withSauce:Yes+]/> Extra Sauce<br />
        <input type="radio" name="cutlery" value="chopSticks"[+cutlery:chopSticks+]/> Chopsticks <input type="radio" name="cutlery" value="Spoon"[+cutlery:Spoon+]/> Spoon
      </p>
      <p>
      	<input type="submit" name="submit" value="Place Order" />
      </p>
      </form>
      


      Example snippet call
      [!eForm? &formid=`testForm` &to=`nobody@anydomain` &gotoid=`1` &tpl=`testForm` &report=`testReport` &format=`yourOrder:Your Order:string:1,withSauce:With Sauce:checkbox:0,cutlery:Cutlery:radio:1`!]


      Part of the formMerge code with modification:
      <?php
      //from formMerge function line 278 - 289
      
      	preg_match_all('~\[\+(.*?)\+\]~', $docText, $matches);
      	for($i=0;$i<count($matches[1]);$i++) {
      		$name = $matches[1][$i];
      		$value = isset($docFields[$name])? $docFields[$name]:"";
      		// listbox, checkbox, radio select
      //mod by TobyL - split name:value pair for checkboxes etc...
      // here's where it goes wrong! $formats["withSauce:Yes"] can never be found as $formats only has
      // the basename as keys and not the extended name:value pairs returned by preg_match_all()
      //the quickest solution (but perhaps not the best) is to add this line
      		list($name,$discard) = explode(":",$name);
      //end mod
      		$fld = $formats[$name]; 
      		if ($fld) {			
      			$datatype = $fld[2];
      			if($datatype=="listbox") $docText = str_replace("[+$name:$value+]","selected='selected'",$docText);
      			if($datatype=="checkbox"||$datatype=="radio") $docText = str_replace("[+$name:$value+]","checked='checked'",$docText);
      		}
      ?>
      


      edited typo...
        • 33372
        • 1,611 Posts
        Quote from: TobyL at May 24, 2006, 08:40 AM

        The problem with  radio and check boxes stems from formMerge() never finding a match for $name in the array $formats.  In formMerge The preg_match_all call on line 287 returns an array with "name:value" for checkboxes and the like but the formats declared in the snippet call only store the "name" as a key.

        One solution is to split the name and value before testing the formats array on line 285.  See the comments in the code below.  For me this works. The form now ’remembers’ checkboxes and radios.
        Aha! What you’re saying makes total sense and looks correct in the function. It still doesn’t work for me however (it keeps replacing my checkbox value flag text with blanks, although other fields are remembered properly).

        If it’s working for you (selected checkboxes, etc. are being "remembered" on error), then I must be doing something wrong. I have edited the  formMerge function and my form and snippet call seem to be correct. Here they are for review:

        Snippet call (broken up so you can read it):
        [!eForm? &vericode=`0` &formid=`application` &to=`[email protected]` &gotoid=`45`
        &tpl=`applicationForm` &report=`applicationReport` &category=`Membership Application`
        &format=`name:Name:string:1,title:Title:string:1,company:Company:string:1,
        street1:Address:string:1,street2:Address:string:0,csz:CSZ:string:0,country:Country:string:0,
        email:Email:email:1,website:Website:string:0,phone:Telephone:string:0,fax:Fax:string:0,
        services:Services:checkbox:0,message:Message:html:1`!]

        formMerge function:
        # Form Merge
        function formMerge($docText, $docFields) {
        	global $formats;
        	$lastitems;
        	if(!docText) return '';
        	preg_match_all('~\[\+(.*?)\+\]~', $docText, $matches);
        	for($i=0;$i<count($matches[1]);$i++) {
        		$name = $matches[1][$i];
        		$value = isset($docFields[$name])? $docFields[$name]:"";
        		// listbox, checkbox, radio select
        //mod by TobyL - split name:value pair for checkboxes etc...
        // here's where it goes wrong! $formats["withSauce:Yes"] can never be found as $formats only has
        // the basename as keys and not the extended name:value pairs returned by preg_match_all()
        //the quickest solution (but perhaps not the best) is to add this line
        		list($name,$discard) = explode(":",$name);
        //end mod
        		$fld = $formats[$name];
        		if ($fld) {
        			$datatype = $fld[2];
        			if($datatype=="listbox") $docText = str_replace("[+$name:$value+]","selected='selected'",$docText);
        			if($datatype=="checkbox"||$datatype=="radio") $docText = str_replace("[+$name:$value+]","checked='checked'",$docText);
        		}
        		if(strpos($name,":")===false) $docText = str_replace("[+$name+]",$value,$docText);
        		else {
        			// this might be a listbox item.
        			// we'll remove this field later
        			$lastitems[count($lastitems)]="[+$name+]";
        		}
        	}
        	$docText = str_replace($lastitems,"",$docText);
        	return $docText;
        }


        My form chunk actually has a snippet in it (that creates the ’services’ checkboxes from the values of a TV), but I have verified that it is adding them as expected (e.g., [+services:Whatever+]). I have also tried with a test group of checkboxes that are not created by a snippet just to be sure (and I did also add this to the snippet call). My test checkbox fields look like this:
        <p><input type="checkbox" name="test" value="One" [+test:One+]/> One</p>
        <p><input type="checkbox" name="test" value="Two" [+test:Two+]/> Two</p>
        <p><input type="checkbox" name="test" value="Three" [+test:Three+]/> Three</p>

        Anyone see why this isn’t working?
          "Things are not what they appear to be; nor are they otherwise." - Buddha

          "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

          Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
          • 30223
          • 1,010 Posts
          With the ’test’ checkboxes, when submitting the form is none of the values remembered or is there perhaps just one, even if you selected multiple choices? When you give all the checkboxes the same value for the name attribue (name="test") the server will always only receive 1 value. So if in the below example all three ’test’ checkboxes are checked and you submit the form the server will only see the value ’Three’. That’s just the nature of the beast...

          <form method="post">
          <input name="formid" type="hidden" value="testForm" />
          <p><input type="checkbox" name="test" value="One" [+test:One+]/> One</p>
          <p><input type="checkbox" name="test" value="Two" [+test:Two+]/> Two</p>
          <p><input type="checkbox" name="test" value="Three" [+test:Three+]/> Three</p>
          <p><input type="submit" value="Submit"/></p>
          </form>
          
          //dump of $_POST with all three checkboxes checked
          Array
          (
              [formid] => testForm
              [test] => Three
              [submit] => Submit
          )
          


          If you want to be able to receive multiple values from these check boxes you need to make "test" into an array by using name="test[]". Adding the square brackets makes php break up ’test’ into an array of selected values. So the checkboxes would look like this:

          <p><input type="checkbox" name="test[]" value="One" [+test:One+]/> One</p>
          <p><input type="checkbox" name="test[]" value="Two" [+test:Two+]/> Two</p>
          <p><input type="checkbox" name="test[]" value="Three" [+test:Three+]/> Three</p>
          
          //dump of $_POST with all three checkboxes checked
          Array
          (
              [formid] => testForm
              [test] => Array
                  (
                      [0] => One
                      [1] => Two
                      [2] => Three
                  )
              [submit] => Place Order
          )
          


          The problem with this however is that eForm doesn’t cater for value arrays. Djamoer has tried some modifications to eForm to cater for this I believe but I haven’t checked them out myself. See http://modxcms.com/forums/index.php/topic,2122.msg19190.html#msg19190

          Hope this helps you along a bit.
            • 33372
            • 1,611 Posts
            Thanks. That does help me a bit. It had crossed my mind that in PHP you need [] after the variable name in order to pass an array, but for some reason the thought left me.

            However, that seems to me to imply that eForm just doesn’t work with checkboxes...period. The whole point of checkboxes is that you can check as many or as few as you like. Otherwise they’d be radio buttons.

            This is for a "need it done yesterday" project, so I’ll probably just make my own form snippet for now. But that seems to be an essential mod to the eForm snippet that someone ought to work on (me if I get the chance). Another mod that I think would really lock this snippet up as THE emailing form script for MODx is if it screened for form spam (header spoofing, etc.), since that is becoming a huge problem on the net.
              "Things are not what they appear to be; nor are they otherwise." - Buddha

              "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

              Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
              • 32963
              • 1,732 Posts
              Hi Everyone,

              Here’s an update to the snippet with the following modifications:
              * Add captcha image support. see [+verimageurl+] - thanks to Djamoer
              * Add multi checkbox, radio, select support - thanks to Djamoer


              To display the captch image just simply add the following to your forms:

              <img src="[+verimageurl+]" />


              Please check and report any errors found.

              ZAP,

              The security on eForm is dependent on PHPMailer library which I think is rock solid. Please make sure you have the latest version of PHPMailer installed in the "manager/includes/controls/" folder.
                xWisdom
                www.xwisdomhtml.com
                The fear of the Lord is the beginning of wisdom:
                MODx Co-Founder - Create and do more with less.
                • 33372
                • 1,611 Posts
                Thanks for the update! Are you working on the checkbox issue as well? If so, then I’ll stop messing with it and wait for your version.

                I wasn’t familiar with PHPmailer and it looks like a great class. I’ll probably start using it for all email scripts now. However I don’t see anything in the docs about form spam checks, so I don’t know whether it deals with that particular nuisance.

                When I say "form spam", I mean specifically when spammers use forms on other people’s websites to send their crapola. Usually this is done by breaking your expected headers (and entering their own header code into the form fields, usually including BCC recipients), so the way to defeat this is to screen your input for this sort of code. I don’t know if PHPmailer does this or not, but I couldn’t find any reference to it doing so.

                If it doesn’t, then there are lots of ways to deal with this. After trying a lot of other things (refuse to send after finding suspicious text like "MIME" and "BCC", cookie-based limits, bad IP lists, etc.), I have started using a much simpler method that seems to be working well so far. I check the email address input to be sure that’s valid, and then for all other fields I replace all @’s with [at].

                This means that if a form spammer tries to abuse my forms there’s no way that they can insert email addresses anywhere they’re not expected, broken headers or not. And if a real live person sends email with @’s in them it doesn’t inconvenience them to see [at] instead. Dunno what others think of my method here, but so far it seems to have run the spammers off my sites and it’s hella easy to implement.
                  "Things are not what they appear to be; nor are they otherwise." - Buddha

                  "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                  Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                  • 25663 MODX Staff
                  • 12,272 Posts
                  cool trick zap ... i like it!
                    Ryan Thrash, MODX Co-Founder
                    Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                    • 32963
                    • 1,732 Posts
                    Hi Zap,

                    The new eform version has the checkbox fixes you can give it a try.

                    As for the spam guard stuff it sounds great!
                      xWisdom
                      www.xwisdomhtml.com
                      The fear of the Lord is the beginning of wisdom:
                      MODx Co-Founder - Create and do more with less.
                      • 33372
                      • 1,611 Posts
                      Howdy -

                      FYI: The snippet in the latest version is missing a # at the beginning of one of the comment lines, which causes it to throw a parse error. Isn’t it always the little things that are the most trouble? Why is that?

                      I still can’t figure out how to make checkboxes work properly with the new version. I have been able to get it to remember one of the checked boxes (meaning I checked three, but only one was remembered), but I can’t get it to remember all of them. To do that the script needs to treat this field as an array, and I need to understand how to pass that to the snippet (do I include the [] in the snippet call, for example?).

                      Any instructions on how to let checkboxes be checkboxes with eForm?

                      Thanks in advance.
                        "Things are not what they appear to be; nor are they otherwise." - Buddha

                        "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                        Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options

                      This discussion is closed to further replies. Keep calm and carry on.