Forum Moderators: coopster
From time to time, maybe every 1-1.5 years, I look up from what I'm doing and think, "so what about this XML stuff"? Then I go off and do a little more reading and learning and then I think, "yah but what can I use it for, what can it do for me"? Up to now, I couldn't come up with an answer so back to the dark recesses it went. Well, this year, I thunk up something which might actually be useful.
How about a script which takes an html form as input and spits out a complete script that will process the form? Oh sure, there's form generators out there, but that's not the point. This is merely an exercise for well-rounded php coders who haven't played with XML much and don't have anything more pressing to do <grin>.
So the challenge is, take this as input:
$sample = <<<EOS
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Herein Lies the Rub</title>
</head>
<body>
<form name="someform" method="post" action="<?php echo \$_SERVER['PHP_SELF'];?>">
<label for="prod">Product <input type="text" name="prod" id="prod" /></label>
<table>
<tr>
<td>Name</td><td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Address</td><td><input type="text" name="address" /></td>
</tr>
<tr>
<td>Telephone</td><td><input type="text" name="telephone" /></td>
</tr>
<tr>
<td>Gender</td>
<td>
<table>
<tr><td><input type="radio" name="gender" value="m" />Male</td></tr>
<tr><td><input type="radio" name="gender" value="f" />Female</td></tr>
<tr><td><input type="radio" name="gender" value="n" />None ya</td></tr>
</table>
</td>
</tr>
</table>
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
EOS;
And produce a php file which will both display and process the form. You can change the action to a placeholder or even remove it entirely if you want - I had to muddy it up to make DOM happy. What you can't do is modify the structure - you gotta deal with that nested table. For extra points, make the resulting script do either an INSERT or an UPDATE, which would include setting the form values to their last known. For more extra points, add in support for other form elements such as textareas, checkboxes, selects, etc. We'll assume the table fields are identical to the form field names, call the table 'sometable' and let's say its primary key is a self-incrementing 'recID'. For an edit, you get $_GET['recID'].
I made the target a heredoc string for brevity, but ultimately you'd probably want your generator script to take either a filename or posted text. The resulting script can either have the html inline or leave it as a variable until output time, whichever way you prefer and will benefit you in the future. You can use all the other tricks in your bag as long as you use DOM or SimpleXML.
The prize is a script that you can use in the future to make one of your many tasks go quicker so you have more time to be bored. Hmm, self-defeating prize. Oh well.
It turned out to be astoundingly simple (just 6 lines of code) although the bruises haven't quite healed from all the head-beating it took to get there. I haven't gone for any of the bonus points yet.
Anyone else rise to the challenge, is it too easy, old-hat, nothing-to-be-learned?