Hello everyone:
I am doing a exercise from Larry Ullman’s book – PHP and MySQL for dynamic web sites. (chapter 3 P119 register.php)
a) I couldn’t figure out the if-else structure around if(empty($errors)) section. please see line 40 and line 49
Please help me to understand how it works. Thanks!
The code:
[code=php]
<?php # Script 3.13 – register.php
/*This script is from the book website
——————————————— */
$page_title = ‘Register’;
include (‘./includes/header.php’);
// Check if the form has been submitted.
if (isset($_POST[‘submitted’])) {
$errors = array(); // Initialize error array.
// Check for a name.
if (empty($_POST[‘name’])) {
$errors[] = ‘You forgot to enter your name.’;
}
// Check for an email address.
if (empty($_POST[’email’])) {
$errors[] = ‘You forgot to enter your email address.’;
}
// Check for a password and match against the confirmed password.
if (!empty($_POST[‘password1’])) {
if ($_POST[‘password1’] != $_POST[‘password2’]) {
$errors[] = ‘Your password did not match the confirmed password.’;
}
} else {
$errors[] = ‘You forgot to enter your password.’;
}
if (empty($errors)) { // If everything’s okay.
// Register the user.
// Send an email.
$body = “Thank you for registering with our site!nYour your password is ‘{$_POST[‘password1′]}’.nnSincerely,nUs”;
mail ($_POST[’email’], ‘Thank you for registering!’, $body, ‘From: [email protected]’);
echo ‘<h1 id=”mainhead”>Thank you!</h1>
<p>You are now registered. An email has been sent to your email address confirming the information.</p><p><br /></p>’;
} else { /* line 40: to me the left curly braces before else is
the end for if(empty($errors)) */
echo ‘<h1 id=”mainhead”>Error!</h1>
<p class=”error”>The following error(s) occurred:<br />’;
foreach ($errors as $msg) { // Print each error.
echo ” – $msg<br />n”;
}
echo ‘</p><p>Please go back and try again.</p><p><br /></p>’;
} /* line 49: the book states here is the End of if (empty($errors)).
it doesn’t make send to me */
} //end of if(post submit function
else { // Display the form.
?>
<h2>Register</h2>
<form action=”register1.php” method=”post”>
<p>Name: <input type=”text” name=”name” size=”20″ maxlength=”40″ /></p>
<p>Email Address: <input type=”text” name=”email” size=”20″ maxlength=”40″ /> </p>
<p>Password: <input type=”password” name=”password1″ size=”10″ maxlength=”20″ /></p>
<p>Confirm Password: <input type=”password” name=”password2″ size=”10″ maxlength=”20″ /></p>
<p><input type=”submit” name=”submit” value=”Register” /></p>
<input type=”hidden” name=”submitted” value=”TRUE” />
</form>
<?php
} // Close the main IF-ELSE.
include (‘./includes/footer.php’);
?>