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.
// 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;
}
}
<?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;
}
?>
<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>
# check vericode
if($vericode) {
if($fields['vericode']!=$_SESSION['veriword']) {
$vMsg[count($vMsg)]="Invalid verification code.";
}
}
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:
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.
&format=`geboortemaand:Geboortemaand:listbox:1
<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>

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.
I there a tutorial or finished project where i could see the usage of this ?
This discussion is closed to further replies. Keep calm and carry on.