PHP Objects in the Insertion Bar

[ LiB ]

Developers can access several objects in the PHP insertion bar. Most of these objects are designed to be used alone. For instance, each object inserts a beginning and ending PHP tag that you need only once in your code block. If you want to hand-code PHP and not use the server behaviors that are detailed in Chapter 23, "Further Dynamic Techniques," you are better off using the tag-completion features of Code view.

For instance, if you start a PHP code block in Code view by typing <?php $HTTP_ , a drop-down list pops up listing all eight HTTP variables : COOKIE_VARS , ENV_VARS , FILES_VARS , GET_VARS , POST_VARS , REQUEST_VARS , SESSION_VARS , and SERVER_VARS , where the objects in the Insertion bar give you only four of the possible options.

The bottom line is that, with PHP, you are better off hand-coding or using the existing server behaviors and whatever server behaviors are available from the community and the Macromedia Exchange.

Form Variables


Whenever a page with a form with a post action calls a PHP page, the variables from the form are put in an array to make them accessible to PHP. The HTTP_POST_VARS array holds each value passed in from the calling form.

The object inserts the following code:

 <?php $HTTP_POST_VARS[];?> 

When you insert the object, you need to enter the name of the post variable that you want to use in quotes. For example, the following code would get the post variable named "department":

 <?php $HTTP_POST_VARS["department"];?> 

Example:

 <?php echo $HTTP_POST_VARS["department"];?> 

This code echoes the value of department to the screen.

URL Variables


Whenever a page with a form with a get action calls a PHP page, the variables from the form are put in an array to make them accessible to PHP. The HTTP_GET_VARS array holds each value passed in from the calling form.

The URL Variables object inserts the following code:

 <?php $HTTP_GET_VARS[];?> 

When you insert the object, you need to enter the name of the get variable that you want to use in quotes. For example, the following code gets the get variable named department :

 <?php $HTTP_GET_VARS["department"];?> 

Example:

 <?php echo $HTTP_GET_VARS["department"];?> 

This code echoes the URL variable value of department to the screen.

Session Variables


Session information keeps information on a visit to a PHP site in an array that can be accessed from page to page or that can be preserved from visit to visit in a database.

 <?php $HTTP_SESSION_VARS[];?> 

When you insert the object, you need to enter the name of the session variable that you want to use in quotes. For example, the following code gets the get variable named visitdate :

 <?php $HTTP_ SESSION_VARS["visitdate"];?> 

Example:

 <?php echo $HTTP_ SESSION_VARS["visitdate "];?> 

This code echoes the session variable value of department to the screen.

Cookie Variables


The Cookie Variables object keeps information that you have saved to or read from the cookie in an array that can be accessed from anywhere in the application.

 <?php $HTTP_COOKIE_VARS[];?> 

When you insert the object, you need to enter the name of the cookie variable that you want to use in quotes. For example, the following code gets the get variable named username :

 <?php $HTTP_ COOKIE_VARS["username"];?> 

Example:

 <?php echo $HTTP_ COOKIE_VARS["username"];?> 

This code echoes the cookie variable value of username to the screen.

Include


The Include object enables you to insert a link to a separate file, the contents of which can be referenced in the page with the include statement. Parts of your logic can be separated into distinct files for many reasons. If you have a piece of functionality such as a shipping calculator or ZIP code locator that you want to access from multiple applications or from multiple locations in one application, this is the tag you want to use. Any variable or function defined in the included file can be referenced in the file that has the include placed in it.

The Include object inserts the following code:

 <?php include(); ?> 

If you were going to include a file called shippingfunction.php, your code would look like this:

 <?php include("shippingfunction.php"); ?> 

Require


The Require Object enables you to insert a link to a separate file, the contents of which can be referenced in the page with the require statement. Unlike the related Include object, if a file that is required is not present, PHP produces a fatal error and stops processing. Parts of your logic can be separated into distinct files for many reasons. Any variable or function defined in the required file can be referenced in the file that has the require placed in it.

The Require object inserts the following code:

 <?php require(); ?> 

If you were going to require a file called shippingfunction.php, your code would look like this:

 <?php require("shippingfunction.php"); ?> 

If the application could not find the shippingfunction.php file, it would throw an error and processing would stop.

PHP Page Encoding


The PHP Page Encoding object adds the mb_http_input and mb_http_output functions to the page. Both functions are used with multibyte input.

The mb_http_input() function detects the HTTP input character encoding. If there is no string specified, it returns the last input type processed .

The mb_http_output() function sets the HTTP output character encoding if the encoding is set in the function. If it is omitted, the function returns the current HTTP output character encoding.

Example:

 mb_http_input("iso-8859-1"); mb_http_output("iso-8859-1"); 

Code Block


The Code Block object inserts a beginning and end delimiter for PHP if you want to hand-code. If you make a selection before inserting, the selection is wrapped in the PHP tag.

 <?php ?> 

Echo


The Echo object inserts an echo language construct (technically, it is not a function) into the page that outputs a string to the browser. You can output multiple strings in a single Echo.

Example:

 $username = "mbrown"; $userpassword = "passmatt"; echo "Username is $username and password is $passmatt" 

This code outputs:

 Username is mbrown and password is passmatt 

Comment


The Comment object inserts comments in the form /*...*/, which you need to do in PHP code blocks. If you have a selection highlighted in Code view, inserting the object wraps the selected text in the comment delimiters.

If and Else


The If object inserts the If control structure to control the flow of the program through the check of some condition. If the condition is true , the enclosed code can be executed. The Else object controls flow by checking for a second condition.

 <?php $a = 14; $b = 12; if( $a > $b ){     print "a is bigger than b"; } else {     print "b is bigger than a"; } ?> 

In this code, there are two variables, a and b , set to different values. In the If statement, the values are checked to see if a is larger than b . If it is, the print statement a is bigger than b is executed. If b is larger than a , the else statement is true and " b is bigger than a " is printed.

More Tags (Advanced)


More PHP tags are available than are displayed in the Insert bar for PHP. When you access the More Tags object, a dialog box opens that lets you pick the other available tags and functions.

Exercise 25.1. Mailing PHP Data

In this exercise, you will use PHP to take data from a form and mail it to you instead of putting it into a database. You might use this functionality to send notification of orders from a shopping cart, or you might set up a form to allow constituents to mail their representatives over a given issue. Because email has become such a ubiquitous form of communication, being able to access it from your applications is a very powerful tool.

Make sure your workstation is properly set up .

You need to have PHP installed and running. MySQL must be installed and running a database; however, for this exercise, you will not actually need the database. You do need to have a PHP site set up in Dreamweaver. You will also need a working local mail server set up and available to use.

SMTP mail is included with Microsoft IIS, and there are versions for other servers. If you have access to an SMTP mail server that allows mail relays, you can use that address. Unfortunately, the capability to relay mail has become less available because it leaves the server open to use by spammers and creates a liability for the ISPs that offer the service.

PHP uses the mail server set up in the PHP.ini file, in the Windows directory of your machine. If you don't find it there, do a search for it: Some versions of Windows place it in other locations.

About halfway through the PHP.ini file, you should find the following lines:

 [mail function] ; For Win32 only. SMTP = localhost ; for Win32 only ; For Win32 only. sendmail_from = me@localhost.com ; for Win32 only ; For Unix only. You may supply arguments as well (default:  "sendmail -t -i"). ;sendmail_path = 

You can leave the SMTP = localhost set as is if you are using your own machine for the mail. If you are using an SMTP mail server that allows you to relay to it, enter that IP address. You should set sendmail from to be your address or an address where the person being mailed can respond.

Creating the Mail Form

You will create a form to capture the information, name, and address for the user to enter to mail to you.

  1. Select File > New to create a new PHP page in your site.

  2. Add a form to the page.

  3. In the form, type Name: and then a space.

  4. Insert a text field into the form on the same line, and name the field name in the Property Inspector.

  5. Press Return to get a new paragraph. Type email address: and a space.

  6. Insert a text field into the form on the same line. Name the field email in the Property Inspector.

  7. Press Return to get a new paragraph. Type Comment: and a space.

  8. Insert a text field into the form on the same line. Name the field comment in the Property Inspector, and set Type to Multiline.

  9. Press Return and enter a Submit button. Your code should look like this:

     <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html;  charset=iso-8859-1"> </head> <body> <form name="form1" method="post" action="mailform.php"> <p>Name:     <input name="name" type="text" id="name"> </p> <p>email address:   <input name="email" type="text" id="email"> </p> <p>Comment:   <textarea name="comment" id="comment"></textarea> </p> <p>   <input type="submit" name="Submit" value="Submit"> </p> </form> </body> </html> 

  10. Save your file as mailform.php .

Creating the Mail Processor

You next will create the page that accepts the information from the form, formats that to an email, and then sends that to you.

  1. Select File > New to create a new PHP page in your site.

  2. Save the page as mailform.php .

  3. Go to Code view of the page and put your cursor in the body.

  4. From the PHP Insert bar, insert a PHP Code Block object. Your cursor will be in the code block, so press Return a couple times to give yourself some space to work.

  5. Type in the following:

     $mail_to = you@youraddress.com; $mail_subject = "Comment from web form"; $mail_name = "Comment from web form"; $mail_body = "Name: "; $mail_body .= 

    In PHP, the = sign assigns a value to a variable. .= appends a value to a variable. You will construct one long variable, piece by piece, that you will enter into the mail.

  6. Insert a Form Variable from the PHP Insert bar. Notice that Dreamweaver has inserted not only the code for the Form Variable, but also a set of begin and end PHP tags. You want to remove those so that your code looks like this:

     $mail_to = you@youraddress.com; $mail_subject = "Comment from web form"; $mail_name = "Comment from web form"; $mail_body = "Name: "; $mail_body .= $_POST[]; 

  7. You need to enter the name of the variable from the form so that the code looks like this:

     <?php $mail_to = you@youraddress.com; $mail_subject = "Comment from web form"; $mail_name = "Comment from web form"; $mail_body = "Name: "; $mail_body .= $_POST['name']; ?> 

  8. For the next line, will add a line break after each variable. Add /n to indicate a new line before each form variable. Type the following code:

     $mail_body .= "\nE-Mail: "; 

    This adds a new line and E-Mail : to the mail_body variable.

  9. Add the following lines to complete the mail_body variable with the values passed in from the form.

     $mail_body .= $_POST['email']; $mail_body .= "\nComment: "; $mail_body .= $_POST['comment']; 

    Your code should now look like this:

     <?php $mail_to ="you@youraddress.com"; $mail_subject = "Comment from web form"; $mail_body =  "Name: "; $mail_body .= $_POST['name'] ; $mail_body .= "\nE-Mail: "; $mail_body .= $_POST['email']; $mail_body .= "\nComment: "; $mail_body .= $_POST['comment']; ?> 

Finally, you want to create the function to mail the variables that you have built here in the mailform.php. You are going to do that, but you will wrap that in an if statement. In PHP, if the mail is sent successfully, the mail() function returns a yes to the flow of the program. If it fails, it sends nothing back. This is important because you want to have some way to know whether the mail was sent or failed. For a production site, you can generate a value in a database or log file, but here you simply show a message onscreen.

  1. On a new line, type the following:

     if(mail($mail_to, $mail_subject, $mail_body))     echo "Mail Sent"; else echo "Mail Failed" 

    This tells PHP to test whether the mail has been sent or and to respond accordingly . The actual function takes three arguments: $mail_to , which is the address for which the mail is intended; $mail_subject , which is the subject of the mail; and $mail_body , which is the body of the mail.

    The entire file should look like this:

     <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html;  charset=iso-8859-1"> </head> <body> <?php $mail_to ="you@yourdomain.com"; $mail_subject = "Comment from web form"; $mail_body =  "Name: "; $mail_body .= $_POST['name'] ; $mail_body .= "\nE-Mail: "; $mail_body .= $_POST['email']; $mail_body .= "\nComment: "; $mail_body .= $_POST['comment']; if(mail($mail_to, $mail_subject, $mail_body))     echo "Mail Sent"; else echo "Mail Failed" ?> </body> </html> 

  2. Save your file.

You are now ready to test your file. Open the form and fill out some information and press the Submit button. You should be taken to a page that gives you a message telling whether the mail was sent. If you run into an error that says you don't have the right to relay mail, you need to set the relay options on your server to allow the localhost machine to send mail. In IIS, that is found in the IIS Administrator, where you need to set that option for the default SMTP server in the SMTP Properties.

[ LiB ]


Macromedia Dreamweaver MX 2004 Demystified
Macromedia Dreamweaver MX 2004 Demystified
ISBN: 0735713847
EAN: 2147483647
Year: 2002
Pages: 188
Authors: Laura Gutman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net