Main Content
Short and sweet and sticky - PHP form input Archive - Originally posted on "The Horse's Mouth" - 2008-02-06 23:13:02 - Graham Ellis
Today I have a short and (very) sweet PHP demo that calls for a user to enter a few pieces of information, and validates them - prior to storage in a database, perhaps.
Why is it "sweet"?
* Because it implements techniques such as sticky fields so that user who make error don't have to keep re-entering
* Because it validates user inputs and gives good error messages
* Because it handles awkward characters, injection attack attempts, etc
* Because it can be shifted from one URL to another without any code changes
* Because it can be easily modified to include any number of extra fields with their validation without having to do lots of low level code
* Because it can be very easily split into three elements - standard functions, a template for the HTML, and the applications top level making it a good foundation for the "4 layer model" approach
Try it out here
See the source here
When you're writing PHP look at each of those "because" clauses and aim to write your code to meet as many of them as possible. Good for the author, good for the user, good for the maintainer.
Some other articles
H115 - Designing PHP-Based Solutions: Best Practice Real life PHP application using our course training MVC example Using an MVC structure - even without a formal framework Learning to program - comments, documentation and test code We not only teach PHP and Python - we teach good PHP and Python Practice! Even early on, separate out your program from your HTML! Filtering PHP form inputs - three ways, but which should you use? PHP sessions - a best practice teaching example Injection Attacks - PHP, SQL, HTML, Javascript - and how to neutralise them Separating program and artwork in PHP - easier maintainance, and better for the user How to build a test harness into your PHP Not just a PHP program - a good web application Adding a newsfeed for your users to a multipage PHP application Improving the structure of your early PHP programs Refactoring - a PHP demo becomes a production page Defensive coding techniques in PHP? PHP Techniques - a workshop This article Software to record day to day events and keep an action list Efficient PHP applications - framework and example A story about benchmarking PHP Ordnance Survey Grid Reference to Latitude / Longitude Converting from postal address to latitude / longitude Controlling and labelling Google maps via PHP Using a MySQL database to control mod_rewrite via PHP Easy handling of errors in PHP Resetting session based tests in PHP Drawing hands on a clock face - PHP Painting a masterpiece in PHP Good Programming practise - where to initialise variables Back button - ensuring order are not submitted twice (PHP) Learning to write secure, maintainable PHP Maintainable code - some positive advice Code quality counts Global, Superglobal, Session variables - scope and persistance in PHP PHP - good coding practise and sticky radio buttons Reporting on the 10 largest files or 10 top scores Giving the researcher power over database analysis Merging pictures using PHP and GD Robust checking of data entered by users A year on - should we offer certified PHP courses Code and code maintainance efficiency Putting a form online Crossfertilisation, PHP to Python Short underground journeys and a PHP book H107 - String Handling in PHP Splitting the difference with PHP Setting up strings in PHP Solution looking for a problem? Lookahead and Lookbehind More than just matching with a regular expression in PHP Getting more than a yes / no answer from a regular expression pattern match Learning to program in PHP - Regular Expression and Associative Array examples Regular Expression modifiers in PHP - summary table PHP - moving from ereg to preg for regular expressions Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP Handling (expanding) tabs in PHP Curly braces within double quoted strings in PHP Handling nasty characters - Perl, PHP, Python, Tcl, Lua Making Regular Expressions easy to read and maintain Finding variations on a surname Regular Expressions in PHP Regular expression for 6 digits OR 25 digits Do not SHOUT and do not whisper This article A taster PHP expression ... Ignore case in Regular Expression Regular Express Primer PHP Regular expression to extrtact link and text Date conversion - PHP Looking ahead and behind in a Regular Expression Evaluating arithmetic expressions in configuration files How similar are two words Don't expose your regular expressions Robust PHP user inputs PHP - dividing a string up into pieces The fencepost problem Converting between acres and hectares Running a Perl script within a PHP page Splitting the difference PHP Magic Quotes the array returned by preg_match_all PHP and natural sorting Here documents