Jump to content
Larry Ullman's Book Forums

Recommended Posts

I am scratching my head over this task. I am quite clueless at this point. I would appreciate help on this.

 

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:

 

<?php

if (isset ($_POST['gender'])) {

$gender = $_POST['gender'];

if ($gender == 'M') {

$greeting = '<p><b>Good day,

Sir!</b></p>';

} elseif ($gender == 'F') {

$greeting = '<p><b>Good day,

Madam!</b></p>';

}

} else {

$gender = NULL;

echo '<p class="error">Gender

should be either "M" or "F"!</p>';

}

Share this post


Link to post
Share on other sites
// Validate the gender:

$gender = 'a'; //create a false value to test against
$validGender = array('M'=>'Sir', 'F'=>'Madam'); // create an associative array of valid values
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
$gender = $_REQUEST['gender']; // store the valid gender
$greeting = '<p><b>Good day, ' . $validGender[$gender] . ' !</b></p>'; // use the valid gender as the key to your array to access the appropriate title
} else { // Unacceptable value.
 $gender = NULL;
 echo '<p class="error">Please select your gender (M/F)!</p>';
 }

  • Upvote 1

Share this post


Link to post
Share on other sites

Hey all.
I've just started reading PHP and MySQL, for Dynamic Web Sites (fourth edition)
I've a problem in regards for solving a pursue goal. 

It is in Chapter 2, and goes like so:

 

Create a form that contains a select

menu or series of check boxes that

allow for multiple sections. Then, in

the handling PHP script, display the

selected items along with a count of

how many the user selected.

I would like to have it all on one page.

So I'm doing something like this
 

 

<?php if($_SERVER["REQUEST_METHOD"] == "POST"){

$formdata = $_POST['interests']; 

foreach($formdata as $interests => $values){
echo $values . " ";    
}
$result = $values;

echo "You have checked " . count($formdata) . " boxes " .
"The boxes were " . $result;
}
?>
<form method="POST" action="own_excercises.php">
<input type="checkbox" name="interests[]" value="music" /> Music
<input type="checkbox" name="interests[]" value="movies" /> Movies
<input type="checkbox" name="interests[]" value="books" /> Books
<input type="checkbox" name="interests[]" value="games" /> Games
<input type="submit" name="sumbit" value="submit" />
</form>  
 

This is what I've come up with this far. I can count the checkboxes check, and in the foreach echo the values of the checkboxes selected, but I can seem to get the values from the foreach in my $result variable. It only display 1 value of the first selected checkbox. I figure I just need a little push in the right direction. If possible it would help a lot to see a different solution to this problem. There are just so many ways to solve the same problem.

Thanks a bunch!
// Archaic

Share this post


Link to post
Share on other sites

To get the total number of checkboxes checked, you want to use count($array). I think a for loop would work better. You'll need to amend the formatting of the output but this should give you the general idea.

<?php if($_SERVER["REQUEST_METHOD"] == "POST"){

$array=array();
if (isset($_POST['interests'])) {
    $array=$_POST['interests'];
    }

$count = count($array);
echo 'You have checked ' . $count . ' boxes, the checked boxes were ';
for ($i=0; $i < $count; $i++) {
    if ($i==($count-1)) {
       echo  "and $array[$i].";
     }
  else {
    echo ' ' .  $array[$i] . ' ';
    }  
 }
}
?>

<form method="POST" action="#">
<input type="checkbox" name="interests[]" value="music" /> Music
<input type="checkbox" name="interests[]" value="movies" /> Movies
<input type="checkbox" name="interests[]" value="books" /> Books
<input type="checkbox" name="interests[]" value="games" /> Games
<input type="submit" name="submit" value="submit" />
</form>

 

  • Upvote 2

Share this post


Link to post
Share on other sites
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.

Edited by MelissaMMDP

Share this post


Link to post
Share on other sites

It's more straightforward than you might be thinking. Here's how it starts:

if ( (isset ($_POST['gender'])) AND ($gender == 'M') ) {

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...