Creating a Form Mail Script
The desired result from the comments form you've been working with in this lesson is to provide a way of sending
The mail Function
PHP's
mail
function sends an email message, using your system's mailer program. On Linux/Unix systems, the
sendmail
utility is used to put a message into the outbound queue. On Windows machines, it usually sends via SMTP, and the
The three required arguments to mail are the recipient's email address, the message subject, and the message body. An optional fourth argument can contain additional mail headers to be sent; this is useful for setting the From: address or adding a Cc: recipient. The script send_comments.php in Listing 11.2 takes the data sent from the comments form and sends it on to the owner of the website by email.
This script
Listing 11.2. send_comments.php
<?php
$body = "These comments were sent via the website\n\n";
foreach($_POST as $field => $value) {
$body .= sprintf("%s = %s\n", $field, $value);
}
mail("owner@website.com", "Comments sent via website", $body,
'From: "WebComments" <comments@website.com>');
?>
<H1>Thank You</H1>
Your comments have been sent!
The email sent to the site owner should look something like the following: The following comments were submitted via the website name = Chris Newman email = chris@lightwood.net gender = m referrer = search may_contact = Y comments = This is my favorite website ever
The format of this email is very rough because each line is generated automatically. Of course, if you prefer, you could
|
Summary
In this lesson you have learned how to process
|
Lesson 12. Generating Dynamic HTML
In this lesson you will learn how to create elements of an HTML form by using PHP. These techniques enable you to specify default values for input items and create dynamic drop-down
|
Setting Default ValuesLet'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 ValuesThe 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
Listing 12.1. Defaulting the Value of a Text Input Field
<?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>
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
Checking a Check BoxThe 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:
<INPUT TYPE="CHECKBOX"
NAME="mybox" <?php if(
condition
) echo "CHECKED";?>>
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 <?php if(
condition
) echo "CHECKED";?>
TYPE="CHECKBOX" NAME="mybox">
Spacing can be very important when you're using PHP within HTML. In the previous example, if there is not a space on either side of the PHP statement and the condition is true, the actual HTML produced is as
<INPUT CHECKEDTYPE="CHECKBOX" NAME="mybox"> 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.
Selecting a Radio Button
|