Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Forms are used everywhere in development to store data in the backend. In PHP, form variables defined in the context are linked to the form elements, and a post-action is created to submit the form in the storage. We will first see some elements that use form validation and deal with the security issue.
Let us create a form with the below-mentioned fields
Name
Must contain only letter and whitespaces and is required
Email
Must be in email format and is required
Profile Link
Should be a link
Message
No rule
Slot
One must be selected
Text Fields
The below code illustrates name, email, profile link, message fields as text input. To take multiple text input lines, one can use textarea and specify the lines of rows and columns they need.
The hacker injected an unwanted script into the page. Now he can exploit information from the site. As he gains access, the javascript code injected is capable to scrap out critical information of the user like passwords, files and card information.
How to avoid it?
Using htmlspecialchars($_SERVER[“PHP_SELF]) can handle such attacks. It works by converting quotations,brackets to HTML entities like " , >, <. Now if the hacker tries to inject his script. The code will get converted to HTML.
The GET method is used to send form data via the URL, making it visible in the browser’s address bar. It is generally used for retrieving data or when the form submission does not involve sensitive information. Parameters are appended to the URL in a query string.
In this example, the form data is sent via the URL, such as process.php?username=value.
PHP Post Form
The POST method is used to send form data to the server in the body of the HTTP request, making it invisible in the URL. It is more secure than GET and is typically used for submitting sensitive data such as passwords or large amounts of data.
In this example, the form data is sent to process.php but is not visible in the URL. The data can be accessed using $_POST in PHP.
Frequently Asked Questions
What is $_POST PHP form?
$_POST is a PHP superglobal array used to collect form data sent via the POST method. It allows secure data submission without displaying it in the URL.
What is $_GET and $_POST in PHP?
$_GET retrieves form data sent via the GET method (visible in the URL), while $_POST retrieves data sent via the POST method (hidden in the request body).
What is $_COOKIE in PHP?
$_COOKIE is a PHP superglobal array that stores data sent by the client’s browser via cookies. It allows data to persist between requests.
Conclusion
We went through setting up a form for validation and used htmlspecialchars to help eliminate exploits. In the next article, you will see how to create error messages, validations in email and much more.
You can take a look at our PHP archives section and see many more interesting topics related to it.
Live masterclass
Amazon PowerBI & AI Essentials: Data Visualization Tips
by Abhishek Soni
01 May, 2025
01:30 PM
Microsoft SDE Roadmap: Use AI Tools to Succeed
by Pranav Malik
28 Apr, 2025
01:30 PM
JioHotstar Sports Analytics: IPL Dataset
by Prerita Agarwal
29 Apr, 2025
01:30 PM
Google SDE interview: Tips to get shortlisted
by Shantanu Shubham
30 Apr, 2025
01:30 PM
Amazon PowerBI & AI Essentials: Data Visualization Tips