PHP for the World Wide Web (Visual QuickStart Guide)
I l @ ve RuBoard |
At the end of Chapter 3 (see Inputting Data Manually), I demonstrated how to use the thinking behind the GET method to send data to a page by appending it to the URL. At that time, I only used this technique to send a variable with a numeric or single word value. We used the same process in Chapter 4, Using Numbers, in much the same way. But what if you want to pass several words as one variable value? For these instances you have the urlencode () function. As its name implies, this function takes a string and encodes it (changes its form) so that it can properly be passed as part of a URL. It replaces spaces with plus signs (+) and translates special characters (for example, the apostrophe) into a less problematic version. The syntax for this function is: $String = urlencode($String); To demonstrate one use of this function, you'll pass your newly created $Name variable to a page which will then greet the user by their full name. Script 5.4. Two things to note: first, the HREF tag is another HTML element that requires use of double quotation marks which you must escape within the print() statement and, second, PHP will replace the $Name variable with its value when sent to the browser (see Figure 5.10).
To use urlencode():
Tip The urldecode() function does just the opposite of the urlencode() it takes an encoded URL and turns it back into a standard form.
Tip Remember that values sent directly from a form are automatically URL-encoded prior to being sent and decoded upon arrival at the receiving script. It is only when you need to manually encode data that the urlencode() function is needed (as in the example).
Tip In Chapter 11, Databases, we'll use functions similar to urlencode() addslashes() and stripslashes(). The former prepares data for entry into a database by escaping problematic characters (single quotation marks, double quotation marks, and the backslash). The later removes the escapes from these same characters. The syntax is exactly as you would expect: $Data = addslashes($Data); $Data = stripslashes($Data); Just as with the urlencode() function, data received from a form is automatically escaped for proper database entry.
|
I l @ ve RuBoard |