This is a demonstration of how you can use XML to feed PHP to generate a form. It is helpful when a flexible form interface is needed. This example includes the name of the field, the length, a regex validation string, default value, label for the input, whether it is required or not, and error text. It could be extended have multilingual validation and error text.
XML:
<?xml version="1.0" encoding="utf-8" ?> | |
<interface> | |
<name>Works</name> | |
<fields> | |
<field> | |
<name>URL</name> | |
<length>255</length> | |
<validation>[\w\.\-]{2,255}</validation> | |
<default>domain.com</default> | |
<label>URL</label> | |
<value>url.com</value> | |
<required>true</required> | |
<errortext>Letters, numbers, periods and dashes only</errortext> | |
</field> | |
<field> | |
<name>id</name> | |
<length>11</length> | |
<validation>[\d]{1,11}</validation> | |
<default>1</default> | |
<label>Id</label> | |
<required>true</required> | |
<errortext>Ids must be all digits</errortext> | |
</field> | |
</fields> | |
</interface> |
This is the PHP that reads the XML. It uses SimpleXML, which is really nice.
PHP:
<?php echo '<?xml version="1.0" encoding="utf-8" ?>' ?> | |
<?php | |
$xml=file_get_contents('bw.xml'); | |
$xmldata = new SimpleXMLElement($xml); | |
$bValid=true; | |
/* Validate the submitted data */ | |
if (isset($_POST['submit'])) | |
{ | |
$bValid=true; | |
foreach ($xmldata->fields->field as $f) | |
if (isset($_POST["{$f->name}"])) | |
{ | |
$f->value=$_POST["{$f->name}"]; | |
if (!preg_match('/^'.$f->validation.'$/',$_POST["{$f->name}"])) | |
{ | |
$f->invalid=(bool)true; | |
$bValid=false; | |
} | |
} | |
} | |
?> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" | |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<title>XML Form</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<style type="text/css"> | |
label | |
{ | |
font-weight:bolder; | |
} | |
input,label | |
{ | |
display:block; | |
} | |
.invalid | |
{ | |
color:#f00; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>XML Form Sourcing Demo</h1> | |
<hr /> | |
<h2>Generated Inputs</h2> | |
<!-- | |
Generates the form from the XML data. Note the use of the invalid flag for highlighting, and the display of | |
errortext if appropriate. | |
--> | |
<form action="#" method="post"> | |
<?php foreach ($xmldata->fields->field as $f) : ?> | |
<label <?php if (isset($f->invalid)) echo 'class="invalid"' ?> for="<?php echo $f->name?>"><?php echo (($f->required== | |
'true')?'*':'').$f->label ?> | |
<?php if (isset($f->invalid)) echo ' '.$f->errortext; ?> | |
<input name="<?php echo $f->name ?>" id="<?php echo $f->name ?>" | |
maxlength="<?php echo $f->length ?>" | |
value="<?php echo (isset($f->value)?$f->value:$f->default) ?>" /> | |
</label> | |
<?php endforeach ?> | |
<br /> | |
<input name="submit" type="submit" /> | |
</form> | |
<h2>Raw XML Data</h2> | |
<!-- Display the raw XML data, for debugging/development --> | |
<blockquote><?php echo nl2br(htmlentities(file_get_contents('bw.xml'))) ?></blockquote> | |
<h2>Parsed XML</h2> | |
<!-- Display how SimpleXML parsed the XML, again for debugging/development --> | |
<?php | |
echo '<pre>'; | |
var_dump($xmldata); | |
echo '</pre>'; | |
?> | |
<h2>Updated XML (if valid)</h2> | |
<!-- Show how the XML was updated, if it was valid --> | |
<?php | |
if ($bValid) | |
echo '<pre>'.nl2br(htmlentities($xmldata->asXML())).'</pre>'; | |
?> | |
</body> | |
</html> |
This is well-suited for applications which must use connection data into a variety of external interfaces. In this case, the data can be presented, entered, validated, and stored, then used later by a different process.