PHP Include Files

The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.

Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

Continue reading PHP Include Files

PHP Date and Time

The PHP date() function is used to format a date and/or a time.

The PHP Date() Function

The PHP date() function formats a timestamp to a more readable date and time. Continue reading PHP Date and Time

PHP Complete Form Example

This chapter shows how to keep the values in the input fields when the user hits the submit button.

PHP – Keep The Values in The Form

To show the values in the input fields after the user hits the submit button, we add a little PHP script inside the value attribute of the following input fields: name, email, and website. In the comment textarea field, we put the script between the <textarea> and </textarea> tags. The little script outputs the value of the $name, $email, $website, and $comment variables. Continue reading PHP Complete Form Example

PHP Forms – Validate E-mail and URL

This chapter shows how to validate names, e-mails, and URLs.

PHP – Validate Name

The code below shows a simple way to check if the name field only contains letters, dashes, apostrophes and whitespaces. If the value of the name field is not valid, then store an error message:

$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
  $nameErr = "Only letters and white space allowed";
}

 

The preg_match() function searches a string for pattern, returning true if the pattern exists, and false otherwise.

Continue reading PHP Forms – Validate E-mail and URL

PHP Forms – Required Fields

This chapter shows how to make input fields required and create error messages if needed

PHP – Required Fields

From the validation rules table on the previous page, we see that the “Name”, “E-mail”, and “Gender” fields are required. These fields cannot be empty and must be filled out in the HTML form.

Field Validation Rules
Name Required. + Must only contain letters and whitespace
E-mail Required. + Must contain a valid email address (with @ and .)
Website Optional. If present, it must contain a valid URL
Comment Optional. Multi-line input field (textarea)
Gender Required. Must select one

In the previous chapter, all input fields were optional. Continue reading PHP Forms – Required Fields

PHP Form Validation

This and the next chapters show how to use PHP to validate form data.

PHP Form Validation

 

Think SECURITY when processing PHP forms!

These pages will show how to process PHP forms with security in mind. Proper validation of form data is important to protect your form from hackers and spammers!

The HTML form we will be working at in these chapters, contains various input fields: required and optional text fields, radio buttons, and a submit button. Continue reading PHP Form Validation

PHP Form Handling

The PHP superglobals $_GET and $_POST are used to collect form-data.

PHP – A Simple HTML Form

The example below displays a simple HTML form with two input fields and a submit button:

Example

<html>
<body>

<form action="welcome.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>

When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named “welcome.php”. The form data is sent with the HTTP POST method. Continue reading PHP Form Handling

PHP Regular Expressions

What is a Regular Expression?

A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this search pattern to describe what you are searching for.

A regular expression can be a single character, or a more complicated pattern.

Regular expressions can be used to perform all types of text search and text replace operations. Continue reading PHP Regular Expressions

PHP – $_GET

PHP $_GET

$_GET contains an array of variables received via the HTTP GET method.

There are two main ways to send variables via the HTTP GET method:

  • Query strings in the URL
  • HTML Forms

Query string in the URL

A query string is data added at the end of a URL. In the link below, everything after the ? sign is part of the query string:

<a href="demo_phpfile.php?subject=PHP&web=W3schools.com">Test $GET</a>

The query string above contains two key/value pairs:

subject=PHP
web=W3schools.com

In the PHP file we can use the $_GET variable to collect the value of the query string. Continue reading PHP – $_GET

PHP – $_POST

PHP $_POST

$_POST contains an array of variables received via the HTTP POST method.

There are two main ways to send variables via the HTTP Post method:

  • HTML forms
  • JavaScript HTTP requests

Continue reading PHP – $_POST