In the previous chapter, we learned about functions and classes. We covered how to define and call functions. We also covered how to define classes and use classes and functions together.
In this chapter, we will focus on handling input from users and printing outcomes back to them, handling errors gracefully, and learning the basics of using the MySQL database.
By the end of this chapter, you will be able to:
Being able to accept input from a user is a major requirement when moving from building websites with PHP to building web applications with PHP. Typically, input comes from HTML forms.
Let's create a simple contact form:
<html> <body> <form action="index.php" method="POST"> <input type="text" name="name" /> <input type="text" name="email" /> <textarea name="message"></textarea> <button type="submit">Send</button> </form> </body> </html>
In the preceding contact form, we see input for a user's name, email, and a message. The method that we are going to use to submit this form is called a POST
request.
To read the data that is being submitted, we are going to add some PHP to the top of the form, which will read and render data from our POST
request:
<?php if($_POST){ echo "Name: " . $_POST['name'] . " "; echo "Email: " . $_POST['email'] . " "; ...... <input type="text" name="email" /> <textarea name="message"></textarea> <button type="submit">Send</button> </form> </body> </html>
As you can see, it's easy to accept input from our application's user. In the preceding code example, we use a special variable,
$_POST
array, to access all of the data that is submitted via a POST
request. The $_POST
variable, an associative array, and the content can be accessed via the names you specify in the input elements.
Another request type that you can use is
GET
request. GET
requests are used more often than you might think; GET
is the request type that you use when you navigate to a website or perform a search on Google. The input for a GET
request is done via a query string.
A query string is a string that is attached to the end of a URL, prepended with a question mark, as seen here: https://www.example.com?name=Michael&age=12:
In the preceding example, you can see that we have two keys that are separated with an ampersand. Just like in the POST
method, there is a special variable for a GET
request, and that is the $_GET
variable (which is an associative array, as well).
If we want to get the name from the previous query string, you can use this line of code:
<?php $name = $_GET['name']; ?>
You can use the GET
request with the form, as well. Let's revisit the form element that we saw before:
<?php if($_GET){ echo "Name: " . $_GET['name'] . " "; echo "Email: " . $_GET['email] . " "; ...... <button type="submit">Send</button> </form> </body> </html>
In the form's method attribute, we changed it to
GET
, swapping the $_POST
variable with the $_GET
variable.
When accepting user input, it's sometimes necessary to clean up the input before doing anything with it. Some input requires cleaning any whitespace from the beginning and end. This is where PHP's trim
function comes in to play. The trim
function will clean whitespace, and other similar characters, from both sides of the user's input. If you want to remove from either the left or right side, you can use the ltrim
and rtrim
functions, respectively.
We are going to start off by building a form for our user list. At the end of this section, you will have a form that will accept your
firstname
, lastname
, and email
. It will have a submit button at the end, to submit the information:
users_list
.index.php
file.index.php
file in your text editor, and add the form code:<html> <body> <form action="index.php" method="post"> ...... </form> </body> </html>
<?php if($_POST){ echo "First Name: " . $_POST['firstname'] . " "; echo "Last Name: " . $_POST['lastname'] . " "; ...... id="email"/> <br> <button type="submit">Save</button> </form> </body> </html>
php -S localhost:8000 -t
With any web application, you are going to need to have a way to store data. The service that permits you to persist your current state in MySQL database is known as persistence. If variables allow you to store data temporarily, persistence allows you to store the data in databases long term.
The primary database type that is used in PHP is MySQL. MySQL databases are known as relational-based databases that are organized into tables.
In this section, we will cover how to use a MySQL database with PHP, and how to perform various operations with it.