Chapter 14

Handling User Input in PHP

IN THIS CHAPTER

  • Displaying all the data in a form
  • Reading data in arrays
  • Creating single-page PHP applications
  • Validating user input
  • Validating numbers
  • Validating text
  • Handling user-inserted HTML

This chapter contains two main parts: using the built-in support in PHP for handling user input (such as working with the powerful server variables that come with PHP and how to determine which browser the user has), and writing code to validate user input.

There's a lot of built-in support in PHP for handling user input, and you're going to see that support in this chapter. Validating user input is also an important part of online programming — checking to make sure what should be an integer really is an integer, or what should be a text string really is a text string, and so on.

Here's the way it'll work in outline. You call a function named validate_data to validate the user's input. If there are any errors, they're stored in an array named $errors. If that array contains any errors, you can call another function, show_errors, to display the errors, and then a display_welcome function to display the application's welcome page so that the user can start over. Otherwise, there are no errors and you can get on with the rest of the application:

validate_data();

if(count($errors) != 0){
    show_errors();
    display_welcome();
}
else {
    handle_data();
}

But more about this later in the chapter. The chapter starts by displaying all of the data in a form, ...

Get Ajax Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.