MelissaMMDP
-
Posts
9 -
Joined
-
Last visited
Posts posted by MelissaMMDP
-
-
Answered my own question:
// Validate the gender:
$gender = $_REQUEST['gender'] ?? NULL;if ($gender == 'M') {
$greeting = '<p><strong>Good day, Sir!</strong></p>';
} elseif ($gender == 'F') {
$greeting = '<p><strong>Good day, Madam!</strong></p>';
} else { // $_POST['gender'] is not set.
echo '<p class="error">You forgot to select your gender!</p>';
} -
Sorry, I just noticed this section. I placed a Pursue question in the general area. I can't delete or move it so if this is something you can do that would be great. Thanks!
-
I've been struggling with the following Pursue challenge. I found an eloquent solution in the forum for version 4 but it included a function which will not be discussed until chapter 3. I just can't find a way to echo a greeting based on gender selection without a nested conditional. I would appreciate help on this.
Pursue:
Rewrite the gender conditional in handle_form.php (Script 2.4) as one conditional instead of two nested ones. Hint: You’ll need to use the AND operator.
Here is the nested conditional:
// Validate the gender:
if (isset($_POST['gender'])) {$gender = $_POST['gender'];
if ($gender == 'M') {
$greeting = '<p><strong>Good day,
Sir!</strong></p>';
} elseif ($gender == 'F') {
$greeting = '<p><strong>Good day,
Madam!</strong></p>';
} else { // Unacceptable value.
$gender = NULL;
echo '<p class="error">Gender
should be either "M" or "F"!
</p>';
}} else { // $_POST['gender'] is not set.
$gender = NULL;
echo '<p class="error">You forgot to
select your gender!</p>';
} -
On 3/12/2012 at 12:42 PM, margaux said:
if (isset($_REQUEST['gender']) && (array_key_exists($_REQUEST['gender'], $validGender))) { //the function array_key_exists lets you check if a value is set as a key in your array
Your solution is eloquent but is this Pursue challenge doable based on what has been learned by the end of Chapter 2? Functions have not been introduced yet. I believe this topic begins in Chapter 3.
Sorry, just realized this question was for version 4 and not version 5. So, it is possible version 4 already discussed functions. I would still like to find a solution that does not require a function. I have not been able to get it to work using only conditionals, operators and arrays.
-
Sorry, I was referring to the Peachpit web edition of your book.
-
Line 6 of Script 2.4 is missing an end quote:
6 <style type="text/css>
should be
6 <style type="text/css">
-
I believe I found an error in the digital online copy. It is in Chapter 2 under "Handling an HTML Form". Paragraph 3 reads:
PHP is case-sensitive when it comes to variable names, so
$_REQUEST[‘city’]
will work, but$_REQUEST[‘city’]
and$_REQUEST[‘city’]
will have no value.I'm not seeing any difference in the three variable names. I believe this sentence needs to be fixed.
-
I also noticed you have an extra "a" in the h2 tag and you need to close this tag.
echo "<p><h2a>Hello $username!</p>";
should be:
echo "<p><h2>Hello $username!</h2></p>";
Chapter 2 Pursue - bullet 3 rewrite gender conditional
in PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (5th Edition)
Posted
Correction - should be:
$gender = $_POST['gender'] ?? NULL;