Monday, February 23, 2009

Handling multiple-choice form elements

To work successfully with forms, you need to know how to handle multiple-choice elements, namely:

1. Radio buttons
2. Check boxes
3. Drop-down option menus
4. Multiple-choice lists

1. Radio buttons:Radio button groups allow you to pick only one value. This makes it easy to retrieve the selected one.All buttons in the same group must share the same name attribute, so the $_POST array contains the value attribute of whichever radio button is selected. If no button is selected, the radio button group’s $_POST array element remains unset. This
is different from the behavior of text input fields, which are always included in the
$_POST array, even if they contain nothing.You need to take this into account in the code that preserves the selected value when a required field is omitted.

For Example in a form we have a radio buttons
<input type="radio" name="testing" value="radiovalue1">
<input type="radio" name="testing" value="radiovalue2">

To retrieve the value we write the code as
1. We will check whether radio button is clicked or not by
$OK = isset($_POST['testing']) ? true : false;
This uses the conditional operator to check whether $_POST['testing'] is set. The
only reason for this line is to avoid having to type isset($_POST['testing']) 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.

2. Then we retrieve by $_POST['testing']

2. Check Boxes : Check boxes are similar to radio button groups, except that they permit multiple selections. This affects how you name a check box group and extract the selected values.
Here we have 2 things we have to remember

1. Check box creates an array.you need to set a default value before attempting to build the body of the email. This time, rather than a string, it needs to be presented as an array like this:
// set default values for variables that might not exist
$subscribe = isset($subscribe) ? $subscribe : 'Nothing selected';
$interests = isset($interests) ? $interests : array('None selected');

2.To extract the values of the check box array, you can use a foreach loop or the implode() function. This oddly named function joins array elements. It takes two arguments: a string to be used as a separator and the array. So, implode(', ', $interests) joins the elements of $interests as a comma-separated string.

<fieldset id="interests">
<h2>Interests in Japan</h2>
<div>
<p>
<input type="checkbox" name="interests[]" value="Anime/manga" id="anime"
<?php
$OK = isset($_POST['interests']) ? true : false;
if ($OK && isset($missing) && in_array('Anime/manga',
$_POST['interests'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="anime">Anime/manga</label>
</p>
<p>
<input type="checkbox" name="interests[]" value="Arts & crafts"
id="art"
<?php
if ($OK && isset($missing) && in_array('Arts & crafts', $_POST['interests'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="art">Arts & crafts</label>
</p>
</div>
</fieldset>

The really important thing to note about this code is the empty pair of square brackets following the name attribute of each check box. This tells PHP to treat interests as an array. If you omit the brackets, $_POST['interests'] contains the value of only the first check box selected; all others are ignored.


OR Simply you can read the Check box value by $_POST['interests']

One thing need to be checked is whether the checkbox are checked or not by:
As with radio buttons, if no check box is selected, the $_POST['interests'] element
is not even created. So the code for the first check box contains the following:
$OK = isset($_POST['interests']) ? true : false;

3.drop-down option menu( SELECT TAG ): Drop-down option menus created with the <select> tag are similar to radio button groups in that they normally allow the user to pick only one option from several. Where they differ is one item is always selected in a drop-down menu, even if it’s only the first item inviting the user to select one of the others. As a result, this means that the $_POST array always contains an element referring to a menu, whereas a radio button group is ignored unless a default value is present.

The following code shows the first two items from the drop-down menu

the PHP code wraps the attribute that indicates which item has been chosen. Although this attribute is called checked in radio buttons and check boxes, it’s called selected in <select> menus and lists. It’s important to use the correct attribute to redisplay the selection if the form is submitted with required items missing. When the page first loads, the $_POST array contains no elements, so you can select the first <option> by testing for !$_POST. Once the form is submitted, the $_POST array always contains an element from a drop-down menu, so you don’t need to test for its existence.

<p>
<label for="select">How did you hear of Japan Journey?</label>
<select name="howhear" id="howhear">
<option value="No reply"
<?php
if (!$_POST || $_POST['howhear'] == 'No reply') { ?>
selected="selected"
<?php } ?>
>Select one</option>
<option value="foED"
<?php
if (isset($missing) && $_POST['howhear'] == 'foED') { ?>
selected="selected"
<?php } ?>
>friends of ED</option>
</select>
</p>

Because there is always an element in the $_POST array for a drop-down menu, it
doesn’t require any special handling in the code that builds the body of the email.

Multiple-choice list(SELECT TAG WITH OPTION SELECTED MULTIPLE)

Multiple-choice lists are similar to check boxes: they allow the user to choose zero or more
items, so the result is stored in an array. If no items are selected, the $_POST array contains no reference to the list, so you need to take that into consideration both in the form and when processing the message.

<p>
<label for="select">What characteristics do you associate with
Japan?</label>
<select name="characteristics[]" size="6" multiple="multiple"
id="characteristics">
<option value="Dynamic"
<?php
$OK = isset($_POST['characteristics']) ? true : false;
if ($OK && isset($missing) && in_array('Dynamic',
$_POST['characteristics'])) { ?>
selected="selected"
<?php } ?>
>Dynamic</option>
<option value="Honest"
<?php
if ($OK && isset($missing) && in_array('Honest',
$_POST['characteristics'])) { ?>
selected="selected"
<?php } ?>
>Honest</option>

</select>
</p>

In the code that processes the message, set a default value for a multiple-choice list
in the same way as for an array of check boxes.

$interests = isset($interests) ? $interests : array('None selected');
$characteristics = isset($characteristics) ? $characteristics : array('None selected');

When building the body of the message, use a foreach loop to iterate through the
subarray, or use implode() to create a comma-separated string like this:
$message .= 'Characteristics associated with Japan: '.implode(', ',$characteristics);


Redirecting to another page

header('Location: http://www.example.com/thanks.php');

When using the header() function, you must be very careful that no output is sent to the
browser before PHP attempts to call it. If, when testing your page, you see an error message
warning you that headers have already been sent, check there are no new lines or other whitespace ahead of the opening PHP tag. Also check any include files for whitespace
and new lines before the opening PHP tag and after the closing one. The error is frequently
triggered by a single new line after the closing tag of an include file.

0 comments:

Post a Comment