With PHP, it's very easy to knock together a page with a couple of radio buttons and a small application just be taking the "bull at a gate" approach - but if you develop code like this, and without planning, you end up with a very long application that's hard to follow and maintain and - unless you've very good at repeatedly debugging the same code - it's going to be unreliable too.
Much better to move all the standard stuff into functions and just keep the data in the main program or even in data files.
To give you an idea, here's a sample I've just written with sticky radio buttons - as a demonstration - showing you how I've separated the mechanism from the actual data:
<?php
function mystickyradio($spec) {
$html = "<tr><td>".htmlspecialchars($spec[0])."</td><td>";
$bname = htmlspecialchars($spec[1]);
for ($k=2; $k<count($spec); $k+=2) {
$chkd = ($_REQUEST[$spec[1]] == $spec[$k+1]) ? " checked" : "";
$html .= "<input type=\"radio\" name=\"$bname\"";
$html .= " value=\"".htmlspecialchars($spec[$k+1])."\"$chkd>";
$html .= htmlspecialchars($spec[$k])."<br>";
}
$html .= "</td></tr>";
return $html;
}
$rb1 = array("Do you do regular weight training?","muscle",
"Occasionally or never","1",
"Yes, as part of my exercise routine","1.2",
"Yes, and I'm a bulky lad","1.5");
$rb2 = array("Select your age range","old",
"Under 21","0-20",
"21 to 65","midlife",
"over 65","old");
$form = "<form method=\"POST\"><table border=1>";
$form .= mystickyradio($rb1);
$form .= mystickyradio($rb2);
$form .= "<tr><td> </td><td><input type=\"submit\"></td></tr>";
$form .= "</table></form>";
?>
<html>
<head>
<title>Data driven sticky radio boxes</title>
</head>
<body>
<h1>Demo Sticky form from parameters</h1>
Please fill in this form<br>
<?= $form ?>
<hr>
Instructions, copyright, etc
</body>
</html>
A real life application would have some "business logic" to actually handle the data entered, of course, and I would move the functions out into a separate include file so that they could be used across all my work. Similarly, the HTML template would be separated out for easier maintainance by an HTML expert. Ah - we're headed for the "4 layer model" again ...
Run the code
here (written 2006-10-17, updated 2006-10-19)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H115 - Designing PHP-Based Solutions: Best Practice [123] Short underground journeys and a PHP book - (2004-11-19)
[237] Crossfertilisation, PHP to Python - (2005-03-06)
[261] Putting a form online - (2005-03-29)
[340] Code and code maintainance efficiency - (2005-06-08)
[394] A year on - should we offer certified PHP courses - (2005-07-28)
[426] Robust checking of data entered by users - (2005-08-27)
[563] Merging pictures using PHP and GD - (2006-01-13)
[572] Giving the researcher power over database analysis - (2006-01-22)
[839] Reporting on the 10 largest files or 10 top scores - (2006-08-20)
[936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
[945] Code quality counts - (2006-11-26)
[1047] Maintainable code - some positive advice - (2007-01-21)
[1052] Learning to write secure, maintainable PHP - (2007-01-25)
[1166] Back button - ensuring order are not submitted twice (PHP) - (2007-04-28)
[1181] Good Programming practise - where to initialise variables - (2007-05-09)
[1182] Painting a masterpiece in PHP - (2007-05-10)
[1194] Drawing hands on a clock face - PHP - (2007-05-19)
[1321] Resetting session based tests in PHP - (2007-08-26)
[1323] Easy handling of errors in PHP - (2007-08-27)
[1381] Using a MySQL database to control mod_rewrite via PHP - (2007-10-06)
[1389] Controlling and labelling Google maps via PHP - (2007-10-13)
[1390] Converting from postal address to latitude / longitude - (2007-10-13)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1482] A story about benchmarking PHP - (2007-12-23)
[1487] Efficient PHP applications - framework and example - (2007-12-28)
[1490] Software to record day to day events and keep an action list - (2007-12-31)
[1533] Short and sweet and sticky - PHP form input - (2008-02-06)
[1623] PHP Techniques - a workshop - (2008-04-26)
[1694] Defensive coding techniques in PHP? - (2008-07-02)
[1794] Refactoring - a PHP demo becomes a production page - (2008-09-12)
[2199] Improving the structure of your early PHP programs - (2009-05-25)
[2221] Adding a newsfeed for your users to a multipage PHP application - (2009-06-06)
[2430] Not just a PHP program - a good web application - (2009-09-29)
[2679] How to build a test harness into your PHP - (2010-03-16)
[3539] Separating program and artwork in PHP - easier maintainance, and better for the user - (2011-12-05)
[3813] Injection Attacks - PHP, SQL, HTML, Javascript - and how to neutralise them - (2012-07-22)
[3820] PHP sessions - a best practice teaching example - (2012-07-27)
[3926] Filtering PHP form inputs - three ways, but which should you use? - (2012-11-18)
[4069] Even early on, separate out your program from your HTML! - (2013-04-25)
[4118] We not only teach PHP and Python - we teach good PHP and Python Practice! - (2013-06-18)
[4326] Learning to program - comments, documentation and test code - (2014-11-22)
[4641] Using an MVC structure - even without a formal framework - (2016-02-07)
[4691] Real life PHP application using our course training MVC example - (2016-06-05)
H110 - PHP - HTML Web Page Data Handling [50] Current cost in your local currency - (2004-09-16)
[589] Robust PHP user inputs - (2006-02-03)
[789] Hot answers in PHP - (2006-07-02)
[1001] .pdf files - upload via PHP, store in MySQL, retrieve - (2006-12-19)
[1053] Sorting people by name in PHP - (2007-01-26)
[1136] Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP - (2007-04-06)
[1169] Emailing as HTML (Web Page) - PHP example - (2007-04-30)
[1831] Text formating for HTML, with PHP - (2008-10-11)
[2025] Injection Attack if register_globals in on - PHP - (2009-02-04)
[2046] Finding variations on a surname - (2009-02-17)
[2107] How to tweet automatically from a blog - (2009-03-28)
[2135] What features does this visitors browser support? (PHP) - (2009-04-22)
[3036] Sending out an email containing HTML from within a PHP page - (2010-11-07)
Some other Articles
Python - function v methodPython - extend v append on a listCourses at Well House ManorToo much for the National TrustPHP - good coding practise and sticky radio buttonsWelcome, MartinWhere quality costs lessVisibilityA pattern in changeWell House Manor Hotel, Melksham, Wiltshire