Free web hosting services - PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY You

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY You need to take this into account in the code that preserves the selected value when a required field is omitted. The following listing shows the subscribe radio button group from contact.php, with all the PHP code highlighted in bold:


Subscribe to newsletter?

checked=”checked” /> checked=”checked” />

The checked attribute in both buttons is wrapped in an if statement, which checks three conditions, all of which must be true. The value of the first condition, $OK, is determined by the following line of code: $OK = isset($_POST[’subscribe’]) ? true : false; This uses the conditional operator to check whether $_POST[’subscribe’] is set. The only reason for this line is to avoid having to type isset($_POST[’subscribe’]) in both if statements. With only two buttons in the radio group, this may hardly seem worthwhile, but I ve used the same technique in all multiple-choice elements, and it certainly makes things easier when you have six items in a group, as is the case with the check boxes and multiple-choice list. The other two conditions inside the if statements check whether $missing has been set and the value of $_POST[’subscribe’]. 2. When building the body of the email message, you also need to take into account that $_POST[’subscribe’] may not exist. Otherwise, you could end up with unprofessional error messages onscreen. Again, using the conditional operator offers the most succinct way of doing this. The following code goes in the section that prepares the message prior to sending it: // go ahead only if not suspect and all required fields OK if (!$suspect && empty($missing)) { // set default values for variables that might not exist $subscribe = isset($subscribe) ? $subscribe : ‘Nothing selected’; If $subscribe exists, the value is simply passed to the same variable. If it doesn t exist, it s set to the string Nothing selected. You can now safely use $subscribe within the body of the message.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

Leave a Reply