I am reading a book and I am in a chapter where it shows how you can create web forms using php.
The problem is that I am getting confused by this switching of the code.
I give you an example :

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
<title>Membership Form</title>
<link rel=”stylesheet” type=”text/css” href=”common.css” />
<style type=”text/css”>
.error { background: #d33; color: white; padding: 0.2em; }
</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();
}
}
function displayForm( $missingFields ) {
?>
<h1>Membership Form</h1>
<?php if ( $missingFields ) { ?>
<p class=”error”>There were some problems with the form you submitted.
Please complete the fields highlighted below and click Send Details to
resend the form.</p>
<?php } else { ?>
<p>Thanks for choosing to join The Widget Club. To register, please
fill in your details below and click Send Details. Fields marked with an
asterisk (*) are required.</p>
<?php } ?>
<form action=”registration.php” method=”post”>
<div style=”width: 30em;”>
<label for=”firstName”<?php validateField( “firstName”,
$missingFields ) ?>>First name *</label>
<input type=”text” name=”firstName” id=”firstName”
value=”<?php setValue( “firstName” ) ?>” />
<label for=”lastName”<?php validateField( “lastName”,
$missingFields ) ?>>Last name *</label>
<input type=”text” name=”lastName” id=”lastName” value=
”<?php setValue( “lastName” ) ?>” />
<label for=”password1”<?php if ( $missingFields ) echo
‘ class=”error”’ ?>>Choose a password *</label>
<input type=”password” name=”password1” id=”password1” value=”” />
<label for=”password2”<?php if ( $missingFields ) echo
‘ class=”error”’ ?>>Retype password *</label>
<input type=”password” name=”password2” id=”password2” value=”” />
<label<?php validateField( “gender”, $missingFields ) ?>>Your
gender: *</label>
<label for=”genderMale”>Male</label>
<input type=”radio” name=”gender” id=”genderMale” value=
”M”<?php setChecked( “gender”, “M” )?>/>
<label for=”genderFemale”>Female</label>
<input type=”radio” name=”gender” id=”genderFemale” value=
”F”<?php setChecked( “gender”, “F” )?> />
<label for=”favoriteWidget”>What’s your favorite widget? *</label>
<select name=”favoriteWidget” id=”favoriteWidget” size=”1”>
<option value=”superWidget”<?php setSelected( “favoriteWidget”,
“superWidget” ) ?>>The SuperWidget</option>
<option value=”megaWidget”<?php setSelected( “favoriteWidget”,
“megaWidget” ) ?>>The MegaWidget</option>
<option value=”wonderWidget”<?php setSelected( “favoriteWidget”,
“wonderWidget” ) ?>>The WonderWidget</option>
</select>
<label for=”newsletter”>Do you want to receive our newsletter?
</label>
<input type=”checkbox” name=”newsletter” id=”newsletter” value=”yes”
<?php setChecked( “newsletter”, “yes” ) ?> />
<label for=”comments”>Any comments?</label>
<textarea name=”comments” id=”comments” rows=”4” cols=”50”><?php
setValue( “comments” ) ?></textarea>
<div style=”clear: both;”>
<input type=”submit” name=”submitButton” id=”submitButton” value=
”Send Details” />
<input type=”reset” name=”resetButton” id=”resetButton”
value=”Reset Form” style=”margin-right: 20px;” />
</div>
</div>
</form>
<?php
}
function displayThanks() {
?>
<h1>Thank You</h1>
<p>Thank you, your application has been received.</p>
<?php
}
?>
</body>
</html>

Is there any way I can make this switching more clear?
I am using aptana studio.
Thank you.

personally, I would never code a form that way. They are doing a lot of php calls when information is set on the form, there is no reason to even use php on that form as all of the validation could be done through javascript or a single php call.
so in this case, I would say there is excessive use of php tags within the page / form itself, that makes it difficult to maintain.
Just a general programming tip. KISS. Keep It Simple Stupid.
The more you can separate you scripting / php from your html the better.
Do the simplest thing to get the job done.
I see absolutely no value to this code:

function setChecked( $fieldName, $fieldValue ) {
if ( isset( $_POST[$fieldName] ) and $_POST[$fieldName] == $fieldValue ) {
echo ‘ checked=”checked”’;
}
}

and how they are using it. I could maybe see a value for it, but not on this form, it is a complete waste.
They could wrap 80% of their functions into one function and validate the form when the submit button is hit. way too much php involved on the page.
My $.02.

The code is from the wrox book Beginning Php 5.3 (Programmer to Programmer).
Do you believe that I should read another book on php?

lol that book sucks, that's pretty hardcore for a starter in PHP, i prefer you learn PHP easy way from w3schools

Thnx for the site suggestion vaultdweller123.
Is there any good book too for php?

i don't really rely on books, i prefer internet sources as they are up to date specially w3schools who gives easy examples

In my opinion w3schools is an excelent resource but stays on a basic level. It is good idea to check how same things are done on php.net, sometimes in examples or comments sections you can find very useful stuff. I also used a book PHP6 and MySql Bible published by Wiley which I found very good (also for PHP 5).

Write HTML tags the normal way when you are outside <?php ... ?> tag. BUt from within you have to use print or echo keyword.

for tutorials I also like:
http://tizag.com/
and for structure , thoughts and ideas I often consult:
http://www.phpclasses.org/
Perhaps read something on MVC (Model-View-Controller) framework.
I'n not a strict adherent of MVC, but the idea itself is a good one. keep as much logic (coding) off of your page as possible.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.