Setting Default Values
Let's begin with some simple examples that embed PHP within form elements to set the default values of some items when the page is loaded.
Default Input Values
The default value of a text input is given in the VALUE attribute. This value displays in the field when the page is loaded, and, unless it is overtyped, the same value is sent to the PHP processing script when the form is submitted.Consider a shopping cart page for an online store, where customers are given the opportunity to change the quantity of each item in their cart before finalizing the order. The current quantity of each line item would be displayed in a small text input box and could be overtyped, and then the user would be able to click a button to refresh the contents of the cart. Listing 12.1 is a very simple example of this, for a store that sells only one product but allows you to choose the quantity to buy.
Listing 12.1. Defaulting the Value of a Text Input Field
First of all, you set an overall default value for $quantity of 1, so that the first time the page is loaded, this is the quantity displayed in the field and used to calculate the total price. Then, inside the VALUE tag, you run a single PHP statement to echo the value of $quantity. If a quantity value is posted to the form, then that value is used instead.This script should be called buy.php so that the form posts to itself when submitted. If you change the quantity value and press the submit button, the script calculates the new total price. Also, the quantity input field defaults to the value you just entered when the page reloads.
<?php
if(isset($_POST["quantity"]))
$quantity = settype($_POST["quantity"], "integer");
else
$quantity = 1;
$item_price = 5.99;
printf("%d x item = $%.2f",
$quantity, $quantity * $item_price);
?>
<FORM ACTION="buy.php" METHOD=POST>
Update quantity:
<INPUT NAME="quantity" SIZE=2
VALUE="<?php echo $quantity;?>">
<INPUT TYPE=SUBMIT VALUE="Change quantity">
</FORM>
Checking a Check Box
The CHECKED attribute determines whether a check box is on or off by default when a page loads. Using PHP, you can embed a condition within the <INPUT> tag to determine whether to include the CHECKED attribute on a check box:
The way this looks can be confusing, particularly because two > symbols appear at the end of the tagone to close the PHP section and one to close the <INPUT> tag. In fact, the position of the CHECKED attribute is not important, so depending on your preference, you can move it around for readability:
<INPUT TYPE="CHECKBOX"
NAME="mybox" <?php if(condition) echo "CHECKED";?>>
<INPUT <?php if(condition) echo "CHECKED";?>
TYPE="CHECKBOX" NAME="mybox">
![]() | Closing PHP Tags When embedding small chunks of PHP, you should always try to include the closing ?> tag as soon as possible. If you miss this closing tag, PHP attempts to interpret the subsequent HTML as PHP and is likely to come up with some interesting error messages! |
Because CHECKEDTYPE is not recognized as part of the <INPUT> tag, your browser is likely to display this as a text input box, not a check box! It's always better to have too much space around dynamic elements in HTML tags than to risk not having enough.
<INPUT CHECKEDTYPE="CHECKBOX" NAME="mybox">
Selecting a Radio Button Group Item
The CHECKED attribute is also used to specify which item in a radio button group should be selected by default. For example, an online store may offer three shipping options, with each having a different cost. To make sure the customer always chooses a shipping option, one of the selections would be picked by default, with the option to change it if desired (a radio button cannot be deselected except when another button in the same group is selected):
To dynamically assign the CHECKED attribute to one of the items in the radio button group, each one must contain a condition that checks the current value of $shipping against the value that corresponds to that item. Listing 12.2 gives an example.
<INPUT TYPE="RADIO" CHECKED
NAME="shipping" VALUE="economy"> Economy
<INPUT TYPE="RADIO" NAME="shipping" VALUE="express"> Standard
<INPUT TYPE="RADIO" NAME="shipping" VALUE="express"> Express
Listing 12.2. Selecting a Default Radio Button Group Item
Notice how cumbersome this is, even for a short radio button group of just three items. Later in this lesson you will learn how to create radio button groups on-the-fly so that larger radio button groups can be managed in a much more elegant way.
<?php
if (!isset($shipping))
$shipping = "economy";
echo "Your order will be sent via $shipping shipping";
?>
<FORM ACTION="shipping.php" METHOD=POST>
<INPUT TYPE="RADIO" NAME="shipping" VALUE="economy"
<?php if ($shipping == "economy") echo "CHECKED";?>> Economy
<INPUT TYPE="RADIO" NAME="shipping" VALUE="standard"
<?php if ($shipping == "standard") echo "CHECKED";?>>
Standard
<INPUT TYPE="RADIO" NAME="shipping" VALUE="express"
<?php if ($shipping == "express") echo "CHECKED";?>> Express
<INPUT TYPE="SUBMIT" VALUE="Change shipping option">
</FORM>
Defaulting a Selection in a Menu
The SELECTED attribute in an <OPTION> tag specifies which item is to be selected by default. If no item has the SELECTED attribute, the first item in the list is shown by default.Using PHP to display the SELECTED attribute against the appropriate option is just as cumbersome as picking the selected item in a radio button group, and the same technique applies. Listing 12.3 shown the same example of shipping rates as in Listing 12.2, using a drop-down menu instead of a radio button group.
Listing 12.3. Selecting a Default Item from a Menu
As with a radio button group, using a function to generate on-the-fly menus allows you to work with much larger option lists and still dynamically select a chosen option.
<?php
if (!isset($shipping))
$shipping = "economy";
echo "Your order will be sent via $shipping shipping";
?>
<FORM ACTION="shipping.php" METHOD=POST>
<SELECT NAME="shipping">
<OPTION <?php if ($shipping == "economy") echo "SELECTED";?>
VALUE="economy">Economy</OPTION>
<OPTION <?php if ($shipping == "standard") echo "SELECTED";?>
VALUE="standard">Standard</OPTION>
<OPTION <?php if ($shipping == "express") echo "SELECTED";?>
VALUE="express">Express</OPTION>
<INPUT TYPE="SUBMIT" VALUE="Change shipping option">
</FORM>