Tag: php forms

PHP Forms Handling

Forms are used to collect data from users. PHP is capable to capture data submitted by users via HTML Forms.

HTML forms are a combination of buttons, checkboxes, and text input fields embedded inside of HTML documents to capture user input.

One of the most powerful features of PHP is the way it handles HTML forms. Once the user enters data and submits a form, the PHP superglobals $_GET and $_POST variables are used to collect that form-data.

PHP can use form data for following things:

  • To simply display user information in the browser
  • To send user query in an email (Ex. Contact Forms)
  • To perform some operation on user data
  • To store user data in the database

PHP Form Example

To understand the working of PHP form, we will create two files. First, an HTML file to create a simple form, and Second a PHP file to receive data from HTML form.

HTML Form Code: form.html
<form action="data.php" method="post">
Name: <input name="name" type="text" />
E-mail: <input name="email" type="text" />
<input type="submit" />
</form>

When the user fills out the form above and clicks the submit button, the form data is sent to PHP file "data.php" mentioned in form's action attribute.

The form data is sent via the HTTP POST method. So, you can use $_POST variable to get the user data in PHP file and simply display data using echo.

Make sure that variable names in $_POST['variable'] match with the mentioned in HTML form element's name.

PHP Form Code: data.php
Welcome <?php echo $_POST["name"]; ?>
<br> Your email address is: <?php echo $_POST["email"]; ?>

Output

The output could be something like this:

[tc_output_window] Welcome Tutorials Class Your email address is tutorialsclass.com@gmail.com[/tc_output_window]