Search the Community
Showing results for tags 'php handle forms'.
-
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>