We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15914
    • 145 Posts
    Is it possible to have an example of how to use Radio and Checkboxes using this snippet? I’m a little confused with how to remember the selected items when returned to the form after a failed validation.

    Thanks in advance.
      Kunal Kapoor
      Technical Director
      Limesharp Internet Limited - Effective, Functional, Standards Compliant

      IRC Nick: KingKoopa
      Skype ID: KingKoopa16
      • 32963
      • 1,732 Posts
      Quote from: Kunal at Jan 27, 2006, 06:43 AM

      Is it possible to have an example of how to use Radio and Checkboxes using this snippet? I’m a little confused with how to remember the selected items when returned to the form after a failed validation.

      Thanks in advance.

      I normally use Javascript for such:

      // peudo code for check a list of checkboxes
      // based on the state variable
      var state=',[+colors+],';
      var f=document.myform;
      chk = f.chkcolors;
      for(i=0;i<chk.length;i++) {
      	if(state.indexOf(','+chk[i].value+',')) {
      		chk[i].checked = true;
      	}
      }

        xWisdom
        www.xwisdomhtml.com
        The fear of the Lord is the beginning of wisdom:
        MODx Co-Founder - Create and do more with less.
        • 32241
        • 1,495 Posts
        Anybody ever use this one with select box, checkbox, radio button, and some other html form list?

        I did, but it seems to me that I couldn’t figure out how to make the form able to remember its last option, after postback, other than using javascript I believe. Another thing is, I figure out that the [+field_name:list_value+] placeholder will not return the "selected=selected" string for choosen input.

        So I decided to change a little bit of the code on function formMerge. Later after I change the function, I realize that I the changes required a little bit modification in receiving the post vars, so I decided to add an if to the POST vars conversion. After changing that thing, I also realize that now the array value need to be imploded in the formMerge function, and I change it again.

        So after tweaking/hacking the code, here is the part that I changed.

        Raymond, I really need your help in here, Honestly, I don’t really have a clue on what this changes will affect the current snippet functionality. Any thought on this, and suggestion/correction in case if I did wrong will be awesome.

        Thanks Raymond.

        <?php
        		# get user post back data
        		foreach($_POST as $name => $value){
        			if(is_array($value))
        				$fields[$name] = $value;
        			else
        				$fields[$name]	= stripslashes(($allowhtml || $formats[$name][2]=='html')? $value:$etomite->stripTags($value));
        		}	
        ?>
        


        <?php
        # 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]:"";
        		if(is_array($value)) $value=implode(', ', $value);
        		// listbox, checkbox, radio select
        		$fld = $formats[$name];
        		if (!isset($fld)) {
        			$colonPost = strpos($name, ':');
        			$listName = substr($name, 0, $colonPost);
        			$listValue = substr($name, $colonPost+1);
        			$datatype = $formats[$listName][2];
        			if(is_array($docFields[$listName])) {
        				if($datatype=="listbox" && in_array($listValue, $docFields[$listName])) $docText = str_replace("[+$listName:$listValue+]","selected='selected'",$docText);
        				if(($datatype=="checkbox"||$datatype=="radio") && in_array($listValue, $docFields[$listName])) $docText = str_replace("[+$listName:$listValue+]","checked='checked'",$docText);
        			}
        			else {
        				if($datatype=="listbox" && $listValue==$docFields[$listName]) $docText = str_replace("[+$listName:$listValue+]","selected='selected'",$docText);
        				if(($datatype=="checkbox"||$datatype=="radio") && $listValue==$docFields[$listName]) $docText = str_replace("[+$listName:$listValue+]","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;
        }
        ?>
        
          Wendy Novianto
          [font=Verdana]PT DJAMOER Technology Media
          [font=Verdana]Xituz Media
          • 32241
          • 1,495 Posts
          I also have a question on how to use the vericode? It’s outputing a generated number, but I have no idea on how to use this for captcha?

          Thanks

          Got it working, but the verification code always invalid, even though I enter the right one.

          Here is what I use
          <div style="margin:10px 0px 10px 0px;">
          <strong>Please enter the verification code below: (to prevent spam)</strong><br />
          <img align="top" src="manager/includes/veriword.php?rand=[+vericode+]" width="148" height="60" alt="If you have trouble reading the code, click on the code itself to generate a new random code." style="margin:5px 0px 5px 0px; border: 1px solid #003399" /><br />
          <input type="text" name="vericode" size="20" />
          </div>
          
            Wendy Novianto
            [font=Verdana]PT DJAMOER Technology Media
            [font=Verdana]Xituz Media
            • 32241
            • 1,495 Posts
            Ok, I got it working, by modifying this code

            		# check vericode
            		if($vericode) {
            			if($fields['vericode']!=$_SESSION['veriword']) {
            				$vMsg[count($vMsg)]="Invalid verification code.";
            			}
            		}
            


            Hope I did it right.
              Wendy Novianto
              [font=Verdana]PT DJAMOER Technology Media
              [font=Verdana]Xituz Media
              • 19726
              • 239 Posts
              Quote from: Djamoer at Feb 06, 2006, 12:40 PM

              Anybody ever use this one with select box, checkbox, radio button, and some other html form list?

              I did, but it seems to me that I couldn’t figure out how to make the form able to remember its last option, after postback, other than using javascript I believe. Another thing is, I figure out that the [+field_name:list_value+] placeholder will not return the "selected=selected" string for choosen input.
              I am using it now with a combobox and it works just fine with the original code. The important thing is to set the correct datatype in the &format parameter. I am using it in the following way:

              In the snippetcall I have:
              &format=`geboortemaand:Geboortemaand:listbox:1


              In the formtemplate I have:
              <select name="geboortemaand"/>
              <option value="januari" [+geboortemaand:januari+]>januari</option>
              <option value="februari" [+geboortemaand:februari+]>februari</option>
              <option value="maart" [+geboortemaand:maart+]>maart</option>
              <option value="april" [+geboortemaand:april+]>april</option>
              <option value="mei" [+geboortemaand:mei+]>mei</option>
              <option value="juni" [+geboortemaand:juni+]>juni</option>
              <option value="juli" [+geboortemaand:juli+]>juli</option>
              <option value="augustus" [+geboortemaand:augustus+]>augustus</option>
              <option value="september" [+geboortemaand:september+]>september</option>
              <option value="oktober" [+geboortemaand:oktober+]>oktober</option>
              <option value="november" [+geboortemaand:november+]>november</option>
              <option value="december" [+geboortemaand:december+]>december</option>
              </select>


              The site is not on-line yet, so I can’t show you a real example but it works fine at my home server.
                • 32241
                • 1,495 Posts
                Hemm...

                That’s one of my problem, too much hacking I believe. When I first try this, it’s not working, so later I decided to get into the code and wrestle with it, and it’s now working just fine, but with a bunch of patching in it lol grin

                For the next site, I will try this eFrom again without hacking the core, maybe it will work this time.

                Btw, the vericode, I think you need to use the patch that I did. I’m not 100% sure, but from reading the code, there is no way it will found a matching vericode, because it’s comparing to the wrong session var. Dunno, I might be totally wrong...
                  Wendy Novianto
                  [font=Verdana]PT DJAMOER Technology Media
                  [font=Verdana]Xituz Media
                  • 32963
                  • 1,732 Posts
                  Quote from: Djamoer at Feb 06, 2006, 03:43 PM

                  Ok, I got it working, by modifying this code

                  		# check vericode
                  		if($vericode) {
                  			if($fields['vericode']!=$_SESSION['veriword']) {
                  				$vMsg[count($vMsg)]="Invalid verification code.";
                  			}
                  		}
                  


                  Hope I did it right.

                  Hi Wendy,

                  Sorry about the delay in responding. I think that should do the trick


                    xWisdom
                    www.xwisdomhtml.com
                    The fear of the Lord is the beginning of wisdom:
                    MODx Co-Founder - Create and do more with less.
                    • 14867
                    • 65 Posts
                    I there a tutorial or finished project where i could see the usage of this ?
                      42
                      • 7455
                      • 2,204 Posts
                      Quote from: JRA at Mar 27, 2006, 04:16 AM

                      I there a tutorial or finished project where i could see the usage of this ?


                      http://modxcms.com/forums/index.php/topic,2122.msg16490.html#msg16490
                        follow me on twitter: @dimmy01

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