Prev | Current Page 212 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

Perhaps one of the most effective examples involves the sanity-checking of
user-supplied form data. Suppose the user is asked to provide six keywords that he
thinks best describe the state in which he lives. A sample form is provided in Listing 5-1.
Listing 5-1. Using an Array in a Form


Provide up to six keywords that you believe best describe the state in
which you live:


Keyword 1:


Keyword 2:


Keyword 3:


Keyword 4:


Keyword 5:


Keyword 6:




CHAPTER 5 ?–  ARRAYS 143
This form information is then sent to some script, referred to as submitdata.php in
the form. This script should sanitize user data and then insert it into a database for later
review. Using array_walk(), you can easily filter the keywords using a predefined
function:
function sanitize_data(&$value, $key) {
$value = strip_tags($value);
}
array_walk($_POST['keyword'],"sanitize_data");
?>
The result is that each value in the array is run through the strip_tags() function,
which results in any HTML and PHP tags being deleted from the value.


Pages:
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224