In this article HTML Forms in PHP we give the information about HTML Forms are used to collect input from users on a website. PHP is used to securely process the input data and send it to the server.

HTML Forms in PHP:

HTML Form is part of a web page, which contains different types of form elements, such as textbox, radio button, dropdown, checkbox etc.

HTML Form Element Types

  1. Text Field

Receives a single text input from the user.

<form method=”POST” action=”form_handler.php”>

    <label for=”name”>Name:</label>

    <input type=”text” id=”name” name=”name”>

    <button type=”submit”>Submit</button>

</form>

Password Field

Receives password input from the user.

<form method=”POST” action=”form_handler.php”>

    <label for=”password”>Password:</label>

    <input type=”password” id=”password” name=”password”>

    <button type=”submit”>Submit</button>

</form>

Radio Buttons

To select only one of the options.

<form method=”POST” action=”form_handler.php”>

    <label for=”gender”>Gender:</label>

    <input type=”radio” id=”male” name=”gender” value=”male”>

    <label for=”male”>Male</label>

    <input type=”radio” id=”female” name=”gender” value=”female”>

    <label for=”female”>female</label>

    <button type=”submit”>Submit</button>

</form>

Check boxes

To select more than one option.

<form method=”POST” action=”form_handler.php”>

    <label for=”hobbies”>Interests:</label>

    <input type=”checkbox” id=”reading” name=”hobbies[]” value=”reading”>

    <label for=”reading”>reading</label>

    <input type=”checkbox” id=”traveling” name=”hobbies[]” value=”traveling”>

    <label for=”traveling”>travelling</label>

    <button type=”submit”>Submit</button>

</form>

Dropdown Select

To select one from more than one option.

<form method=”POST” action=”form_handler.php”>

    <label for=”city”>City:</label>

    <select id=”city” name=”city”>

        <option value=”delhi”>Delhi</option>

        <option value=”mumbai”>Mumbai</option>

        <option value=”kolkata”>Kolkata</option>

    </select>

    <button type=”submit”>Submit</button>

</form>

textarea

To receive multidisciplinary input from the user.

<form method=”POST” action=”form_handler.php”>

    <label for=”message”>Message:</label>

    <textarea id=”message” name=”message”></textarea>

    <button type=”submit”>Submit</button>

</form>

File Upload

To upload file from user.

<form method=”POST” action=”form_handler.php” enctype=”multipart/form-data”>

    <label for=”file”>Upload file:</label>

    <input type=”file” id=”file” name=”file”>

    <button type=”submit”>Submit</button>

</form>

Handling Form Data in PHP

When a form is submitted by a user, PHP processes this data through a server-side script.

Form Handler (PHP Code)

<?php

//to handle the form

if ($_SERVER[“REQUEST_METHOD”] == “POST”) {

    $name = $_POST[‘name’];

    $password = $_POST[‘password’];

    $gender = $_POST[‘gender’];

    $hobbies = $_POST[‘hobbies’];

    $city = $_POST[‘city’];

    $message = $_POST[‘message’];

// example format

    echo “Name: $name<br>”;

    echo “Password: $password<br>”;

    echo “Gender: $gender<br>”;

    echo “Interests: ” . implode(“, “, $hobbies) . “<br>”;

    echo “City: $city<br>”;

    echo “Message: $message<br>”;

}

?>

Entertainment for Form (Validation and Sanitization)

Use validation and sanitization to secure and clean form data.

Sanitization and verification code

<?php

if ($_SERVER[“REQUEST_METHOD”] == “POST”) {

    $name = htmlspecialchars($_POST[‘name’]); // HTML sanitization

    $password = htmlspecialchars($_POST[‘password’]);

    $gender = $_POST[‘gender’];

    $hobbies = $_POST[‘hobbies’];

    $city = $_POST[‘city’];

    $message = htmlspecialchars($_POST[‘message’]); // HTML sanitization

//other action

    echo “Name: $name<br>”;

}

?>

Conclusion

HTML Forms are the most common way to get data from the user.

PHP is used to process and handle form data on the server.

Various types of elements like textfield, radio button, checkbox, dropdown etc. are used for forms.

Verification and sanitization are necessary to handle form data securely and correctly.

Getting user input from $_GET and $_POST

$_GET and $_POST are PHP superglobal variables, which are used to receive data from the user. Their main difference is in the way data is sent:

  1. $_GET: Sends the data via URL query string.
  2. $_POST: Sends the data via HTTP POST request.

$_GET

$_GET is used to retrieve data from the user through a GET request. The data is appended to the URL.

Examples of $_GET

<?php

//Getting data using $_GET

if ($_SERVER[“REQUEST_METHOD”] == “GET”) {

    $name = $_GET[‘name’];

    echo “Name: $name”;

}

?>

form html

<form method=”GET” action=””>

    <label for=”name”>Name:</label>

    <input type=”text” id=”name” name=”name”>

    <button type=”submit”>Submit</button>

</form>

  1. $_POST

$_POST is used to receive data from the user through POST request. The data is sent encoded in the POST request and is not visible on the URL of the web page.

Examples of $_POST

<?php

// Fetching data using $_POST

if ($_SERVER[“REQUEST_METHOD”] == “POST”) {

    $name = $_POST[‘name’];

    echo “Name: $name”;

}

?>

form html

<form method=”POST” action=””>

    <label for=”name”>Name:</label>

    <input type=”text” id=”name” name=”name”>

    <button type=”submit”>Submit</button>

</form>

Main differences between $_GET and $_POST

$_GET $_POST

– Data is sent via URL query string.   – Data is sent via HTTP POST request.

– Limited character length (2000-3000).         – More secure (handles unique signals).

– Cannot send sensitive information (such as passwords).     – Safe to send sensitive information.

Example of using both for form handling

Form handling with $_GET and $_POST

<?php

if ($_SERVER[“REQUEST_METHOD”] == “POST”) {

    $name = $_POST[‘name’];

    echo “POST name: $name”;

}

if ($_SERVER[“REQUEST_METHOD”] == “GET”) {

    $name = $_GET[‘name’];

    echo “GET name: $name”;

}

?>

Usage examples of $_GET and $_POST

  1. Use of $_GET:

When information has to be shared through URL.

For non-sensitive data such as search queries.

  1. Use of $_POST:

To send sensitive data like passwords, credentials etc.

For longer information.

tips for safety

Use $_POST when exchanging sensitive information.

Use $_GET for limited information, including information that needs to be shown with a URL.

Verify and sanitize form data to protect against form attacks.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *