This time round I decided that all that cutting and pasting of form tags into the php page (and their layout) was a waste of time so I created a couple of database tables to tell the template which fields to include. So given the form name the template creates the rest of the page.
<?php
$sql="select form_caption, update_file, ok_function, validate_function
from schema_forms
where form_name = '$FormName';
select field_name, field_type, field_caption, span_name, needs_validation, can_hide, select_sql, allow_blank,
block_numbers, allow_decimal, allow_negative
from schema_form_fields
where form_name = '$FormName'
order by field_number;";
if (!isset($link)) {
include("connectioni.php");
}
if (!mysqli_multi_query($link,$sql)) {
echo "<p>" . mysqli_error($link) . ". Unable to load property list.</p>";
}
else {
do {
if ($result = mysqli_store_result($link)) {
$finfo = mysqli_fetch_field($result);
if ($finfo->name=="form_caption") {
$row=mysqli_fetch_array($result, MYSQLI_BOTH);
$FormCaption=$row["form_caption"];
$UpdateFile=$row["update_file"];
$OKFunction=$row["ok_function"];
$ValidateFunction=$row["validate_function"];
} else {
?>
<p class="popuptitle"><?=$FormCaption?></p>
<form name="<?=$FormName?>">
<input name="UpdateFile" value="<?=$UpdateFile?>" type="hidden"/>
<table class="popuptable">
<?php
while ($row=mysqli_fetch_array($result, MYSQLI_BOTH)) {
$FieldName=$row["field_name"];
$FieldType=$row["field_type"];
$FieldCaption=$row["field_caption"];
$SpanName=$row["span_name"];
$SpanCaptionName=$SpanName."caption";
$SpanInputName=$SpanName."input";
$NeedsValidation=$row["needs_validation"];
$CanHide=$row["can_hide"];
$SelectSql=$row["select_sql"];
$AllowBlank=$row["allow_blank"];
$BlockNumbers=$row["block_numbers"];
$AllowDecimal=$row["allow_decimal"];
$AllowNegative=$row["allow_negative"];
if ($FieldType!="Hidden") {
echo "<tr>\n";
echo "<td class=\"popuptable\">\n";
if ($CanHide=="Y") {
echo "<span id=\"$SpanCaptionName\">";
}
echo $FieldCaption;
if ($CanHide=="Y") {
echo "</span>";
}
if ($NeedsValidation=="Y") {
echo "<span id=\"$SpanName\"></span>";
}
echo "</td>\n";
echo "<td class=\"popuptable\">\n";
if ($CanHide=="Y") {
echo "<span id=\"$SpanInputName\">";
}
}
switch ($FieldType) {
case 'Hidden':
echo "<input name=\"$FieldName\" value=\"".$$FieldName."\" type=\"hidden\"/>";
break;
case 'Text':
echo "<input class=\"popmeinput\" size=\"15\" name=\"$FieldName\" value=\"".$$FieldName."\" type=\"text\"";
if ($NeedsValidation=="Y") {
echo " onKeyup=\"$ValidateFunction();\" onblur=\"$ValidateFunction();\"";
}
if ($BlockNumbers=="Y") {
echo " onKeyPress=\"return blockNonNumbers(this, event, $AllowDecimal, $AllowNegative);\"";
}
echo "/>";
break;
case 'Textarea':
echo "<textarea class=\"popmeinput\" name=\"$FieldName\"";
if ($NeedsValidation=="Y") {
echo " onKeyup=\"$ValidateFunction();\" onblur=\"$ValidateFunction();\"";
}
echo ">".$$FieldName."</textarea>";
break;
case 'Password':
echo "<input class=\"popmeinput\" size=\"15\" name=\"$FieldName\" value=\"".$$FieldName."\" type=\"password\"";
if ($NeedsValidation=="Y") {
echo " onKeyup=\"$ValidateFunction();\" onblur=\"$ValidateFunction();\"";
}
echo "/>";
break;
case 'Date':
$FieldNameD=$FieldName."D";
$FieldNameM=$FieldName."M";
$FieldNameY=$FieldName."Y";
echo "<input class=\"popmeinput\" size=\"1\" name=\"$FieldNameD\" value=\"".$$FieldNameD."\" type=\"text\"";
if ($NeedsValidation=="Y") {
echo " onKeyup=\"$ValidateFunction();\" onblur=\"$ValidateFunction();\"";
}
echo "/>";
echo "<select class=\"popmeinput\" name=\"$FieldNameM\"";
if ($NeedsValidation=="Y") {
echo " onChange=\"$ValidateFunction();\" onBlur=\"$ValidateFunction();\"";
}
echo ">\n";
echo "<option value=\"\"></option>\n";
for ($i=1; $i<=12;$i++) {
if ($i==$$FieldNameM) {
$Selected="selected=\"selected\"";
} else {
$Selected="";
}
echo "<option $Selected value=\"$i\">";
echo date("M",mktime(0,0,0,$i,1,2000));
echo "</option>\n";
}
echo "</select>";
echo "<input class=\"popmeinput\" size=\"2\" name=\"$FieldNameY\" value=\"".$$FieldNameY."\" type=\"text\"";
if ($NeedsValidation=="Y") {
echo " onKeyup=\"$ValidateFunction();\" onblur=\"$ValidateFunction();\"";
}
echo "/>";
break;
case 'Select':
echo "<select class=\"popmeinput\" name=\"$FieldName\"";
if ($NeedsValidation=="Y") {
echo " onChange=\"$ValidateFunction();\" onBlur=\"$ValidateFunction();\"";
}
echo ">\n";
if ($AllowBlank=="Y") {
echo "<option></option>\n";
}
if (!isset($link2)) {
include("connection2i.php");
}
if (mysqli_multi_query($link2,$SelectSql)) {
do {
if ($result2 = mysqli_store_result($link2)) {
while ($row2=mysqli_fetch_array($result2, MYSQLI_BOTH)) {
$ExistingFieldName=$row2[0];
if (isset($row2[1])) {
$ExistingFieldID=$row2[1];
if ($ExistingFieldID==$$FieldName) {
$Selected="selected=\"selected\"";
}
else {
$Selected="";
}
$ExistingFieldID='value="'.$ExistingFieldID.'"';
} else {
unset($ExistingFieldID);
if ($ExistingFieldName==$$FieldName) {
$Selected="selected=\"selected\"";
}
else {
$Selected="";
}
}
echo "<option $Selected $ExistingFieldID>$ExistingFieldName</option>\n";
}
mysqli_free_result($result2);
}
} while (mysqli_next_result($link2));
}
echo "</select>";
break;
}
if ($FieldType!="Hidden") {
if ($CanHide=="Y") {
echo "</span>";
}
echo "</td>\n";
echo "</tr>\n";
}
}
?>
<tr>
<td class="popuptable">
<span id="aOK"><a class="popuptable" href="#" onclick="<?=$OKFunction?>">OK</a></span>
</td>
</tr>
</table>
</form>
<form name="OnloadForm">
<input name="OnloadFunction" value="<?=$ValidateFunction?>();" type="hidden"/>
</form>
<?php
}
mysqli_free_result($result);
}
} while (mysqli_next_result($link));
}
?>
Then he tells me that there was probably a tool out there that would have done it for me! So then you just need to set the variable $FormName, include this file and write the appropriate Javascript validation function. I Ajax the lot into a floating box in the middle of the screen (pretending to be a modal dialogue box).