Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP Form Handling

PHP – A Simple HTML Form

Below is an example of a simple HTML form featuring 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” using the HTTP POST method.

To display the submitted data, you can use PHP to echo all the variables.

The content of “welcome.php” is as follows:

<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>

Here’s an example of what the output could look like:

Welcome John
Your email address is john.doe@example.com

The same result could also be achieved using the HTTP GET method:

Example

Here’s the same example, but with the method set to GET instead of POST:

<html>
<body>
<form action="welcome_get.php" method="GET">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
input type="submit">
</form>
</body>
</html>

and the “welcome_get.php” file looks like this:

<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>

The code above is quite simple and does not include any validation.

It is essential to validate form data to protect your script from malicious code.

Think SECURITY when processing PHP forms!

This page does not include any form validation; it only demonstrates how to send and retrieve form data.

However, the following pages will show how to process PHP forms with security in mind. Proper validation of form data is crucial to protect your forms from hackers and spammers!