Page 281 - Beginning PHP 5.3
P. 281
Chapter 9: Handling HTML Forms with PHP
</style>
</head>
<body>
<?php
if ( isset( $_POST[“submitButton”] ) ) {
processForm();
} else {
displayForm( array() );
}
function validateField( $fieldName, $missingFields ) {
if ( in_array( $fieldName, $missingFields ) ) {
echo ‘ class=”error”’;
}
}
function setValue( $fieldName ) {
if ( isset( $_POST[$fieldName] ) ) {
echo $_POST[$fieldName];
}
}
function setChecked( $fieldName, $fieldValue ) {
if ( isset( $_POST[$fieldName] ) and $_POST[$fieldName] == $fieldValue ) {
echo ‘ checked=”checked”’;
}
}
function setSelected( $fieldName, $fieldValue ) {
if ( isset( $_POST[$fieldName] ) and $_POST[$fieldName] == $fieldValue ) {
echo ‘ selected=”selected”’;
}
}
function processForm() {
$requiredFields = array( “firstName”, “lastName”, “password1”,
“password2”, “gender” );
$missingFields = array();
foreach ( $requiredFields as $requiredField ) {
if ( !isset( $_POST[$requiredField] ) or !$_POST[$requiredField] ) {
$missingFields[] = $requiredField;
}
}
if ( $missingFields ) {
displayForm( $missingFields );
} else {
displayThanks();
}
}
243
9/21/09 7:23:41 PM
c09.indd 243
c09.indd 243 9/21/09 7:23:41 PM