PHP forms allow you to collect data from users and send it to a server for processing. Forms are typically used to submit information such as names, email addresses, and comments, or to make selections from a list of options.
Here is an example of a simple form in PHP:
<form action="process.php" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
<input type="submit" value="Submit">
</form>
This form has three fields: a text field for the user’s name, an email field for the user’s email address, and a submit button. When the user fills out the form and clicks the submit button, the form data is sent to the server-side script specified in the action
attribute (in this case, process.php
). The method
attribute specifies the HTTP method that will be used to submit the form data (in this case, post
).
To process the form data on the server side, we can use the $_POST
superglobal array in PHP. The $_POST
array contains the form data as key-value pairs, where the keys are the names of the form fields and the values are the user-submitted data. Here is an example of how to access the form data in the process.php
script:
$name = $_POST['name'];
$email = $_POST['email'];
echo "Name: $name<br>Email: $email";
This script outputs the name and email that the user entered in the form.
In addition to the post
method, forms can also use the get
method to submit data. The get
method appends the form data to the URL as a query string, which can be accessed on the server side using the $_GET
superglobal array. Here is an example of a form using the get
method:
<form action="process.php" method="get">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
<input type="submit" value="Submit">
</form>
To process the form data in the process.php
script, we can use the $_GET
array in the same way as the $_POST
array:
$name = $_GET['name'];
$email = $_GET['email'];
echo "Name: $name<br>Email: $email";
In addition to text fields and email fields, there are many other types of form elements that you can use in your PHP forms. Here are some examples:
- Radio buttons: Radio buttons allow the user to select one option from a group of options. They are often used when there are multiple mutually exclusive choices. Here is an example of how to use radio buttons in a form:
<form action="process.php" method="post">
<label>Gender:</label><br>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other<br><br>
<input type="submit" value="Submit">
</form>
In this example, the user can select one of the three gender options by clicking on the corresponding radio button. The checked
attribute specifies which option is selected by default. To access the selected value in a PHP script, you can use the $_POST
array as usual:
$gender = $_POST['gender'];
echo "Gender: $gender<br>";
- Checkboxes: Checkboxes allow the user to select multiple options from a group of choices. They are often used when there are multiple optional choices. Here is an example of how to use checkboxes in a form:
<form action="process.php" method="post">
<label>Interests:</label><br>
<input type="checkbox" name="interests[]" value="arts"> Arts<br>
<input type="checkbox" name="interests[]" value="sports"> Sports<br>
<input type="checkbox" name="interests[]" value="technology"> Technology<br><br>
<input type="submit" value="Submit">
</form>
In this example, the user can select one or more of the interests by clicking on the corresponding checkboxes. Notice that the name
attribute of the checkboxes is an array (interests[]
). This is because multiple checkboxes can have the same name
, and we want to store all of the selected values in an array. To access the selected values in a PHP script, you can use the $_POST
array as usual:
$interests = $_POST['interests'];
echo "Interests:<br>";
foreach ($interests as $interest) {
echo "- $interest<br>";
}
- Select lists: Select lists allow the user to select one or more options from a drop-down list. They are often used when there are many options and it is not practical to display them all at once. Here is an example of how to use a select list in a form:
<form action="process.php" method="post">
<label for="country">Country:</label><br>
<select name="country" id="country">
<option value="usa">USA</option>
<option value="canada">Canada</option>
<option value="mexico">Mexico</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
In this example, the user can select one of the three countries by clicking on the drop-down list and choosing an option. The value
attribute of the option
elements specifies the value that will be sent to the server when the form is submitted.
To access the selected value in a PHP script, you can use the $_POST
array as usual:
$country = $_POST['country'];
echo "Country: $country<br>";
You can also allow the user to select multiple options from the select list by using the multiple
attribute:
<form action="process.php" method="post">
<label for="countries">Countries:</label><br>
<select name="countries[]" id="countries" multiple>
<option value="usa">USA</option>
<option value="canada">Canada</option>
<option value="mexico">Mexico</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
In this example, the user can select one or more countries by holding down the CTRL
key (or CMD
key on a Mac) and clicking on the options. The selected values will be stored in an array on the server side, which you can access using the $_POST
array:
$countries = $_POST['countries'];
echo "Countries:<br>";
foreach ($countries as $country) {
echo "- $country<br>";
}
Select lists are a useful way to present a large number of options to the user in a compact and easy-to-use form element. You can find more information about select lists and other form elements in the PHP documentation.
It is important to validate and sanitize form data before using it, to prevent security vulnerabilities such as cross-site scripting (XSS) attacks. You can use functions such as filter_input
and htmlspecialchars
to validate and sanitize form data. Here is an example of how to do this:
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
echo "Name: " . htmlspecialchars($name) . "<br>";
echo "Email: " . htmlspecialchars($email) . "<br>";
In this example, we use the filter_input
function to sanitize the form data and the htmlspecialchars
function to escape any HTML characters that may be present in the data. This helps to prevent XSS attacks by ensuring that user-submitted data is not interpreted as HTML by the browser.