dino2007 0 Posted March 12, 2012 Report Share Posted March 12, 2012 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>'; } Quote Link to post Share on other sites
margaux 171 Posted March 12, 2012 Report Share Posted March 12, 2012 // 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>'; } 1 Quote Link to post Share on other sites
Archaic 0 Posted February 17, 2013 Report Share Posted February 17, 2013 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 Quote Link to post Share on other sites
margaux 171 Posted February 17, 2013 Report Share Posted February 17, 2013 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> 2 Quote Link to post Share on other sites
MelissaMMDP 0 Posted December 12, 2018 Report Share Posted December 12, 2018 (edited) 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 December 12, 2018 by MelissaMMDP Quote Link to post Share on other sites
Larry 429 Posted December 15, 2018 Report Share Posted December 15, 2018 It's more straightforward than you might be thinking. Here's how it starts: if ( (isset ($_POST['gender'])) AND ($gender == 'M') ) { Quote Link to post Share on other sites
Venkok 0 Posted July 10, 2020 Report Share Posted July 10, 2020 On 3/12/2012 at 9:42 PM, margaux said: // 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>'; } Hi! Why do we need this: $gender = 'a'; //create a false value to test against The script works well without it, please explain! Quote Link to post Share on other sites
Larry 429 Posted July 10, 2020 Report Share Posted July 10, 2020 I wouldn't say that line is necessary. The $gender variable is already assigned a value before being used so assigning it a "false" value has no impact. 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.