Inputting Data Manually

I l @ ve RuBoard

Obviously, not all of your data will derive from HTML forms and sometimes you might want to manually set data within your script. If you created a script that printed a user 's name in a greeting, you could establish a variable that contains the name separate from the rest of the greeting. That way you could easily change the name without having to change the greeting, the print statement, or the script itself. We will create a PHP script to demonstrate this process.

Script 3.5. Instead of coding the page to automatically say "Hello, World!" or "Hello, Larry!" we have made the page dynamic by printing out the value of a variable instead. Now, as that value changes, so will the resulting page.

graphics/03sc05.jpg

To creating a PHP script that displays a greeting:

  1. Create a new document within your text editor (Script 3.5).

     <HTML><HEAD><TITLE>Using Variables   </TITLE><BODY><?php /* This page may have numerous lines   of code containing the following   print statement. */ 

    I have put two comments into the script that are meant to represent the idea that the greeting line will mostly likely be part of a larger, more complicated page.

     print "<H2> <CENTER> <B> Hello,  $FirstName. </B> </CENTER> </H2>  <BR>\n"; /* This page may have more code   after the print statement we are  focusing on. */ ?></BODY></HTML> 
  2. Save the document as hello.php and upload it to your server.

If you were to view this script now, it would only display "Hello, ." as the $FirstName variable has no value. There are two ways you can manually set this value, without using forms. The first is to use what you know about how the GET method passes data to a script.

To use the GET method without an HTML form:

  1. View the hello.php script in your Web browser by going to the appropriate URL (in this case, http://www.DMCinsights.com/ php/hello.php (Figure 3.6).

    Figure 3.6. Without the $FirstName variable being assigned a value, the browser prints out this awkward message.

    graphics/03fig06.jpg

  2. Append to the URL the text ?FirstName=Larry (you can also use your own name, just ensure that there are no spaces in your entry).

    As you read earlier in this chapter (Receiving Data from a Form in PHP), when you send a variable to a script via a URL (i.e., by using the GET method in an HTML form), it uses the format of

     www.url.com/script.php?variable=  value. 
  3. Reload the page in your browser with the new URL (Figure 3.7).

    Figure 3.7. By setting $FirstName equal to Larry or some other name, you've created a dynamic page that changes to address the specific user.

    graphics/03fig07.jpg

    If you do not see the name printed in the browser, then you made a mistake. Check to make sure that you included the question mark, which separates the file location from the data itself. Then see that you used FirstName, as that is what hello.php is expecting as a variable (remember that firstname would constitute a different variable).

The second way you can preset a variable value is by directly assigning it within the script itself.

Script 3.6. The $FirstName = "Jude"; line assigns the value "Jude" to the variable "FirstName" for the existence of this page.

graphics/03sc06.jpg

To assign a value to a variable:

  1. Open hello.php in your text editor.

  2. On a line before the print statement, add $FirstName = "Jude"; (Script 3.6).

  3. Save your page, upload it to your server, and view it in your Web browser (Figure 3.8).

    Figure 3.8. Setting the value of $FirstName to Jude, will have the same effect as changing the print line to read print "<H2> <CENTER> <B> Hello, Jude. </B> </CENTER> </H2> <BR>\n";, but it will be easier for us to change the value by using a variable instead.

    graphics/03fig08.gif

  4. Now view the same page using the appended version of the URL as you did in the last example (e.g., http://www.DMCinsights.com/php/hello.php?FirstName=Larry) (Figure 3.9).

    Figure 3.9. Since the page itself contains a line assigning the value "Jude" to $FirstName, no matter what you pass to the script via the URL, Jude will always be printed.

    graphics/03fig09.jpg

    This demonstrates an important point as to what happens when a variable is assigned a value multiple times: the last assigned value is what the variable becomes "worth" and, consequently in our case, what hello.php will print out. Be careful not to inadvertently override your variables in your programming!

The concept of pre-establishing variable values is especially useful as your program gets more complicated. Using an easily edited variable means that you never have to hunt through multiple lines of code to replace one value with another.

Likewise, by appending a variable to a URL, you can link one page to a dynamically generated second page (such as hello.php/ ) simply by coding your Web page inks to include variable values, where necessary. For example, one form on a Web site may take the user's first name, and pass that along to subsequent linked pages with the code <A HREF ="hello.php? FirstName=Larry">hello.php </A>.

Tip

If you want to use the GET method to send data to your script, you can pass multiple values along by separating the variable=value pairs (e.g., FirstName= Larry ) with the ampersand (&). So an appended URL may be hello.php? FirstName=Larry&LastName=Ullman .


Tip

Spaces within values that are passed as part of a URL should be replaced with a plus sign (+). In Chapter 5, Using Strings, I'll discuss how PHP can automatically prepare a string of text to be passed as part of a URL.


Tip

Although the example heresetting the value of a person's namemay not be very practical, creating an $email variable at the top of the script would allow you to later change that email address without looking through lines of code to find it. In fact, in Chapter 11, Databases, I'll make it a habit to establish the database access values through variables at the beginning of our scripts so they can be altered easily without changing every instance where those values are used.


I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

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