Search the Community
Showing results for tags 'conditionals'.
-
Hello! Please explain to me how gender validations from scripts 2.3 differ from the nested one in script 2.4. I have this in 2.3 (updated with NULL coalescing operator while practicing Pursue section): $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 { $gender = NULL; echo '<p class="error">Gender must be either "M" or "F"!</p>'; /* You may wonder how this last case may be possible, considering the values are set in the HTML form. If a malicious user creates their own form that gets submitted to your handle_form.php script (which is very easy to do), they could give $_REQUEST[‘gender’] any value they want. */ } and this in 2.4: if (isset($_REQUEST['gender'])) { $gender = $_REQUEST['gender']; if ($gender = 'M') { $greeting = '<p><strong>Good day, Sir!</strong></p>'; } elseif ($gender = 'F') { $greeting = '<p><strong>Good day, Madam!</strong></p>'; } else { $gender = NULL; echo '<p class="error">Gender must be either "M" or "F"!</p>'; /* You may wonder how this last case may be possible, considering the values are set in the HTML form. If a malicious user creates their own form that gets submitted to your handle_form.php script (which is very easy to do), they could give $_REQUEST[‘gender’] any value they want. */ } } else { // $_REQUEST['gender'] is not set. $gender = NULL; echo '<p class="error">You forgot to select your gender!</p>'; } It seems that these scripts do the same job, or I just can't figure out the difference, please help me to understand it. And also I want to if we could use NULL coalescing operator in script 2.4 some way. Thank you!
- 1 reply
-
- nested
- null coalescing operator
- (and 6 more)
-
Placing this in 4th Edition as that's the book I'm working through. I've created a form with three inputs (Agent, Guest, Email) but Agent is not required. I'm trying to display different messages whether Agent is input or not but cannot get that part to work. I know the reset button doesn't work but that's just as the form was copied from an old html page. I'll get into field content validation once I can get past this stage as I have multiple fields to add but need to display different messages on form submission dependent on which fields get completed. <!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content="text/html" charset="utf-8"> <title>frm_request_home</title> </head> <body> <?php // Start input validation: // Validate the Agent: if (!empty($_POST['agent'])) { $agent = $_POST['agent']; } else { $agent = NULL; } // Validate the Guest Name: if (!empty($_POST['guest'])) { $guest = $_POST['guest']; } else { $guest = NULL; echo '<p class="error"> You forgot to enter the guest name!</p>'; } // Validate the Email Address: if (!empty($_POST['email'])) { $email = $_POST['email']; } else { $guest = NULL; echo '<p class="error"> You forgot to enter your email address!</p>'; } // Conditional to check for form submission: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // form validation: if (!isset($_POST['agent']) ) { // Print the results if Agent: echo "<p>Thank you <b>$agent</b> for your request for <b>$guest</b>.</p> <p>We will reply to you at <i>$email</i>.</p>\n"; // Print the results if Guest: } elseif (!empty($_POST['guest']) && !empty($_POST['email']) ) { echo "<p>Thank you, <b>$guest</b>, for your request.</p> <p>We will reply to you at <i>$email</i>.</p>\n"; } else { // missing form value. echo '<p class"error"> Please fill out the form again.</p>'; } } // Leave the PHP section and create the HTML form: ?> <table width="98%" border="0" align="center" cellpadding="2" cellspacing="0" class="tblBorder"> <tr> <td align="center" class="txtHeaderRev">Request Form</td> </tr> <tr> <td> <form action="frm_request_home.php" method="post"> <table width="86%" border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td align="right" class="txtMain">Agency Name (if applicable):</td> <td><input type="text" name="agent" value="<?php if (isset($_POST['agent'])) echo $_POST['agent']; ?>" style="width:250px;" /></td> </tr> <tr> <td width="46%" align="right" class="txtMain">Guest Name :</td> <td><input type="text" name="guest" value="<?php if (isset($_POST['guest'])) echo $_POST['guest']; ?>" style="width:250px;" /></td> </tr> <tr> <td align="right" class="txtMain">Email Address:</td> <td><input type="text" name="email" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" style="width:250px;" /></td> </tr> <tr> <td> </td> <td> <input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /> </td> </tr> <tr> <td> </td> <td> </td> </tr> </table> </form> </td> </tr> </table> </body> </html>
-
The Switch Conditional exercise on pages 143 - 150 makes us of an undefined variable, var cost; and I'm not sure what role it plays in the code. Do the cost elements in the switch statements load the value into the undefined variable var cost depending upon which condition is met in the document's type value? I can remove these two lines of code, as well as the undefined variable, var cost, and still see the membership form run: var type = document.getElementById('type'); var years = document.getElementById('years'); Why did we create a reference to the document element years when it seems not to be used in the script? Many mysteries have unfolded in this exercise.