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...?
<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>
[!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`!]
<?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);
}
?>
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).
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.
[!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`!]
# 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;
}<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>
<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
)
<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
)
* Add captcha image support. see [+verimageurl+] - thanks to Djamoer
* Add multi checkbox, radio, select support - thanks to Djamoer
<img src="[+verimageurl+]" />
This discussion is closed to further replies. Keep calm and carry on.